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.

Property definitions

core :: abstract_text $ CString :: to_s_unsafe
	# 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 is abstract
lib/core/text/abstract_text.nit:2527,2--2549,104

core :: flat $ CString :: to_s_unsafe
	redef fun to_s_unsafe(byte_length, char_length, copy, clean)
	do
		byte_length = byte_length or else cstring_length
		clean = clean or else true
		copy = copy or else true

		# Clean?
		var str = null
		if clean then
			str = clean_utf8(byte_length)
			char_length = str.length
		else
			char_length = char_length or else utf8_length(0, byte_length)
		end

		# Copy? (if not already copied by `clean_utf8`)
		if copy and (str == null or str.items == self) then
			var new_cstr = new CString(byte_length + 1)
			copy_to(new_cstr, byte_length, 0, 0)
			new_cstr[byte_length] = 0
			str = new FlatString.full(new_cstr, byte_length, 0, char_length)
		end

		if str == null then
			str = new FlatString.full(self, byte_length, 0, char_length)
		end

		return str
	end
lib/core/text/flat.nit:1315,2--1343,4