# Copy `self` to `dest`.
fun copy_to(dest: NativeString, length: Int, from: Int, to: Int) is intern
+ redef fun ==(o) is intern do return is_same_instance(o)
+
+ redef fun !=(o) is intern do return not is_same_instance(o)
+
# Position of the first nul character.
fun cstring_length: Int
do
return endpos
end
- # Number of UTF-8 characters in `self` between positions `from` and `to`
- fun utf8_length(from, to: Int): Int do
+ # Number of UTF-8 characters in `self` starting at `from`, for a length of `bytelen`
+ fun utf8_length(from, bytelen: Int): Int is intern do
var st = from
- var lst = to
var ln = 0
- while st <= lst do
- st += length_of_char_at(st)
+ while bytelen > 0 do
+ while bytelen >= 4 do
+ var i = fetch_4_chars(st)
+ if i & 0x80808080 != 0 then break
+ bytelen -= 4
+ st += 4
+ ln += 4
+ end
+ if bytelen == 0 then break
+ var cln = length_of_char_at(st)
+ st += cln
ln += 1
+ bytelen -= cln
end
return ln
end
# Fetch 4 chars in `self` at `pos`
- fun fetch_4_chars(pos: Int): Int is intern do return fetch_4_ffi(pos)
+ fun fetch_4_chars(pos: Int): Int is intern `{ return (long)*((uint32_t*)(self+pos)); `}
# Fetch 4 chars in `self` at `pos`
- fun fetch_4_hchars(pos: Int): Int is intern do return fetch_4h_ffi(pos)
-
- # FIXME: To remove when bootstrap supports PR #1898
- private fun fetch_4_ffi(pos: Int): Int `{ return (long)*((uint32_t*)(self+pos)); `}
- private fun fetch_4h_ffi(pos: Int): Int `{ return (long)be32toh(*((uint32_t*)(self+pos))); `}
+ fun fetch_4_hchars(pos: Int): Int is intern `{ return (long)be32toh(*((uint32_t*)(self+pos))); `}
end