Merge: Revamp the C string to Nit string services to put forward the safest alternative
authorJean Privat <jean@pryen.org>
Wed, 18 Jan 2017 13:42:19 +0000 (08:42 -0500)
committerJean Privat <jean@pryen.org>
Wed, 18 Jan 2017 13:42:19 +0000 (08:42 -0500)
There are now three services to convert a `CString` to a `String`:

* `CString::to_s` and `to_s_with_length(byte_length)` copies data into the GC memory and clean the UTF-8 characters. The difference is that `to_s` search for the first null byte to find the length of the string. These services are the safest and recommended for use with the FFI. They replace the old `to_s_with_copy` and `to_s_with_copy_and_length`.

* `CString::to_s_unsafe(byte_length, char_length, copy, clean)` is the unsafe alternative. It can optimize conversion of a string that has already been cleaned, is already in the GC memory or of which the number of characters has already been counted. This service unifies (and replace) the old `to_s`, `to_s_with_length`, `to_s_full` and `to_s_unsafe`.

Also fix `from_percent_encoding` that did not clean its output, same for the binary deserialization of single characters, and `environ` which now copies the C string into GC memory.

Update a test result as there is now one more string (the copy by `environ`) and a call to `String::length` to cut on the calls to `CString::utf8_length`.

This PR applies #2057 and it is based on #2347. That a look at the commit "core: revamp `CString::to_s` services" first.

Edit: The full doc and signature of `to_s_unsafe` for ease of reading:

# Get a `String` from the data at `self` (with unsafe options)
#
# The default behavior is the safest and equivalent to `to_s`.
#
# Options:
#
# * Set `byte_length` to the number of bytes to use as data.
#   Otherwise, this method searches for a terminating null byte.
#
# * Set `char_length` to the number of Unicode character in the string.
#   Otherwise, the data is read to count the characters.
#   Ignored if `clean == true`.
#
# * If `copy == true`, the default, copies the data at `self` in the
#   Nit GC allocated memory. Otherwise, the return may still point to
#   the data at `self`.
#
# * If `clean == true`, the default, the string is cleaned of invalid UTF-8
#   characters. If cleaning is necessary, the data is copied into Nit GC
#   managed memory, whether or not `copy == true`.
#   Don't clean only when the data has already been verified as valid UTF-8,
#   other library services rely on UTF-8 compliant characters.
fun to_s_unsafe(byte_length, char_length: nullable Int, copy, clean: nullable Bool): String

Pull-Request: #2350
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>

1  2 
lib/core/bytes.nit
lib/core/text/abstract_text.nit
lib/mongodb/mongodb.nit

Simple merge
Simple merge
Simple merge