8b66384b4def433f398d12dfd2e7e802116fe147
[nit.git] / lib / standard / text / native.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
9 # another product.
10
11 # Native structures for text and bytes
12 module native
13
14 import kernel
15
16 # Native strings are simple C char *
17 extern class NativeString `{ char* `}
18 # Creates a new NativeString with a capacity of `length`
19 new(length: Int) is intern
20
21 # Returns a char* starting at `index`.
22 #
23 # WARNING: Unsafe for extern code, use only for temporary
24 # pointer manipulation purposes (e.g. write to file or such)
25 fun fast_cstring(index: Int): NativeString is intern
26
27 # Get char at `index`.
28 fun [](index: Int): Byte is intern
29
30 # Set char `item` at index.
31 fun []=(index: Int, item: Byte) is intern
32
33 # Copy `self` to `dest`.
34 fun copy_to(dest: NativeString, length: Int, from: Int, to: Int) is intern
35
36 # Position of the first nul character.
37 fun cstring_length: Int
38 do
39 var l = 0
40 while self[l] != 0u8 do l += 1
41 return l
42 end
43
44 # Parse `self` as an Int.
45 fun atoi: Int is intern
46
47 # Parse `self` as a Float.
48 fun atof: Float `{ return atof(self); `}
49 end