note
	description: "A low-level string class to solve some garbage collector problems (mainly objects moving around) when interfacing with C APIs."
	library: "Free implementation of ELKS library"
	status: "See notice at end of class."
	legal: "See notice at end of class."
	date: "$Date: 2020-05-19 14:23:13 +0000 (Tue, 19 May 2020) $"
	revision: "$Revision: 104257 $"

class 
	C_STRING

create 
	make,
	make_empty,
	make_by_pointer,
	make_by_pointer_and_count,
	make_shared_from_pointer,
	make_shared_from_pointer_and_count,
	own_from_pointer,
	own_from_pointer_and_count

feature {NONE} -- Initialization

	default_create
			-- Process instances of classes with no creation clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end

	make (a_string: READABLE_STRING_GENERAL)
			-- Make a C string from a_string.
		require
			a_string_not_void: a_string /= Void
			a_string_is_valid_as_string_8: a_string.is_valid_as_string_8
		do
			make_empty (a_string.count)
			set_string (a_string)
		end

	make_by_pointer (a_ptr: POINTER)
			-- Make a copy of string pointed by a_ptr.
		require
			a_ptr_not_null: a_ptr /= default_pointer
		do
			make_by_pointer_and_count (a_ptr, c_strlen (a_ptr))
		end

	make_by_pointer_and_count (a_ptr: POINTER; a_length: INTEGER_32)
			-- Make a copy of first a_length byte of string pointed by a_ptr.
		require
			a_ptr_not_null: a_ptr /= default_pointer
			a_length_non_negative: a_length >= 0
		do
			count := a_length
			create managed_data.make (a_length + 1);
			managed_data.item.memory_copy (a_ptr, a_length)
		end

	make_empty (a_length: INTEGER_32)
			-- Make an empty C string of a_length characters.
			-- C memory area is not initialized.
		require
			a_length_positive: a_length >= 0
		do
			create managed_data.make (a_length + 1)
			count := 0
		end

	make_shared_from_pointer (a_ptr: POINTER)
			-- New instance sharing a_ptr.
		require
			a_ptr_not_null: a_ptr /= default_pointer
		do
			make_shared_from_pointer_and_count (a_ptr, c_strlen (a_ptr))
		end

	make_shared_from_pointer_and_count (a_ptr: POINTER; a_length: INTEGER_32)
			-- New instance sharing a_ptr of a_length byte.
		require
			a_ptr_not_null: a_ptr /= default_pointer
			a_length_non_negative: a_length >= 0
		do
			count := a_length
			create managed_data.share_from_pointer (a_ptr, a_length + 1)
		end

	own_from_pointer (a_ptr: POINTER)
			-- New instance using a_ptr as memory. Current will free pointed memory
			-- by a_ptr when collected.
		require
			a_ptr_not_null: a_ptr /= default_pointer
		do
			own_from_pointer_and_count (a_ptr, c_strlen (a_ptr))
		end

	own_from_pointer_and_count (a_ptr: POINTER; a_length: INTEGER_32)
			-- New instance using a_ptr as memory. Current will free pointed memory
			-- by a_ptr when collected.
		require
			a_ptr_not_null: a_ptr /= default_pointer
			a_length_non_negative: a_length >= 0
		do
			count := a_length
			create managed_data.own_from_pointer (a_ptr, a_length)
		end
	
feature -- Initialization

	set_shared_from_pointer (a_ptr: POINTER)
			-- Share a_ptr.
		require
			a_ptr_not_null: a_ptr /= default_pointer
		do
			set_shared_from_pointer_and_count (a_ptr, c_strlen (a_ptr))
		end

	set_shared_from_pointer_and_count (a_ptr: POINTER; a_length: INTEGER_32)
			-- Share a_ptr of a_length byte.
		require
			a_ptr_not_null: a_ptr /= default_pointer
			a_length_non_negative: a_length >= 0
		do
			count := a_length
			if not managed_data.is_shared then
				create managed_data.share_from_pointer (a_ptr, a_length + 1)
			else
				managed_data.set_from_pointer (a_ptr, a_length + 1)
			end
		end

	share_from_pointer (a_ptr: POINTER)
		obsolete "Use `make_shared_from_pointer' to create object and `set_shared_from_pointer' to modify it. [2017-05-31]"
			-- New instance sharing a_ptr.
		require
			a_ptr_not_null: a_ptr /= default_pointer
		do
			set_shared_from_pointer_and_count (a_ptr, c_strlen (a_ptr))
		end

	share_from_pointer_and_count (a_ptr: POINTER; a_length: INTEGER_32)
		obsolete "Use `make_shared_from_pointer_and_count' to create object and `set_shared_from_pointer_and_count' to modify it. [2017-05-31]"
			-- New instance sharing a_ptr of a_length byte.
		require
			a_ptr_not_null: a_ptr /= default_pointer
			a_length_non_negative: a_length >= 0
		do
			set_shared_from_pointer_and_count (a_ptr, a_length)
		end
	
feature -- Access

	copy_to_string (a_string: STRING_GENERAL; source_index, destination_index, n: INTEGER_32)
			-- Copy n characters of Current from source_index position to a_string at
			-- destination_index. Other characters of a_string remain unchanged.
		require
			a_string_not_void: a_string /= Void
			source_index_positive: source_index >= 1
			destination_index_positive: destination_index >= 1
			n_non_negative: n >= 0
			n_is_small_enough_for_source: source_index + (n - 1) <= capacity
			n_is_small_enough_for_destination: destination_index + (n - 1) <= a_string.count
		local
			l_data: like managed_data
			i, j, nb: INTEGER_32
		do
			from
				i := source_index - 1
				j := destination_index
				nb := source_index + (n - 1)
				l_data := managed_data
			until
				i = nb
			loop
				a_string.put_code (l_data.read_natural_8 (i).to_natural_32, j)
				i := i + 1
				j := j + 1
			end
		end

	generating_type: TYPE [detachable C_STRING]
			-- Type of current object
			-- (type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generating_type_not_void: Result /= Void
		end

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generator_not_void: Result /= Void
			generator_not_empty: not Result.is_empty
		end

	item: POINTER
			-- Get pointer to allocated area.
		do
			Result := managed_data.item
		ensure
			item_not_null: Result /= default_pointer
		end

	managed_data: MANAGED_POINTER
			-- Hold data of Current.

	read_string_into (a_string: STRING_GENERAL)
			-- Copy of substring containing all characters at indices
			-- between start_pos and end_pos into a_string replacing any
			-- existing characters.
		require
			a_string_not_void: a_string /= Void
			a_string_large_enough: a_string.count >= count
		do
			read_substring_into (a_string, 1, count)
		end

	read_substring_into (a_string: STRING_GENERAL; start_pos, end_pos: INTEGER_32)
			-- Copy of substring containing all characters at indices
			-- between start_pos and end_pos into a_string.
		require
			a_string_not_void: a_string /= Void
			start_position_big_enough: start_pos >= 1
			end_position_big_enough: start_pos <= end_pos + 1
			end_position_not_too_big: end_pos <= capacity
			a_string_large_enough: a_string.count >= end_pos - start_pos + 1
		local
			l_data: like managed_data
			i, j, nb: INTEGER_32
		do
			from
				i := start_pos - 1
				nb := end_pos - start_pos
				l_data := managed_data
				j := 1
			until
				i > nb
			loop
				a_string.put_code (l_data.read_natural_8 (i).to_natural_32, j)
				i := i + 1
				j := j + 1
			end
		end

	read_substring_into_character_32_area (a_area: SPECIAL [CHARACTER_32]; start_pos, end_pos: INTEGER_32)
			-- Copy of substring containing all characters at indices
			-- between start_pos and end_pos into a_area.
		require
			a_area_not_void: a_area /= Void
			start_position_big_enough: start_pos >= 1
			end_position_big_enough: start_pos <= end_pos + 1
			end_position_not_too_big: end_pos <= capacity
			a_area_large_enough: a_area.count >= end_pos - start_pos + 1
		local
			l_data: like managed_data
			i, j, nb: INTEGER_32
		do
			from
				i := start_pos - 1
				nb := end_pos - start_pos
				l_data := managed_data
			until
				i > nb
			loop
				a_area.put (l_data.read_natural_8 (i).to_character_32, j)
				i := i + 1
				j := j + 1
			end
		ensure
			copied: 
		end

	read_substring_into_character_8_area (a_area: SPECIAL [CHARACTER_8]; start_pos, end_pos: INTEGER_32)
			-- Copy of substring containing all characters at indices
			-- between start_pos and end_pos into a_area.
		require
			a_area_not_void: a_area /= Void
			start_position_big_enough: start_pos >= 1
			end_position_big_enough: start_pos <= end_pos + 1
			end_position_not_too_big: end_pos <= capacity
			a_area_large_enough: a_area.count >= end_pos - start_pos + 1
		do
			managed_data.read_into_special_character_8 (a_area, start_pos - 1, 0, end_pos - start_pos + 1)
		ensure
			copied: 
		end

	read_unicode_substring_into_character_32_area (a_area: SPECIAL [CHARACTER_32]; start_pos, end_pos: INTEGER_32)
			-- Copy of Unicode substring containing all characters at indices
			-- between start_pos and end_pos into a_area.
		require
			a_area_not_void: a_area /= Void
			start_position_big_enough: start_pos >= 1
			end_position_big_enough: start_pos <= end_pos + 1
			end_position_not_too_big: end_pos <= capacity
			a_area_large_enough: a_area.count >= end_pos - start_pos + 1
		local
			l_data: like managed_data
			i, j, nb: INTEGER_32
			uc: NATURAL_32
		do
			from
				i := start_pos - 1
				nb := end_pos - start_pos
				l_data := managed_data
			until
				i > nb
			loop
				uc := l_data.read_natural_32 (i);
				a_area.put (uc.to_character_32, j)
				i := i + 4
				j := j + 1
			end
		end

	string: STRING_8
			-- Eiffel string, ignoring count. Reads until a null character is being read.
		do
			Result := substring (1, c_strlen (item))
		ensure
			string_not_void: Result /= Void
		end

	string_8: STRING_8
			-- Eiffel string, ignoring count. Reads until a null character is being read.
		do
			Result := substring_8 (1, c_strlen (item))
		ensure
			string_not_void: Result /= Void
		end

	substring (start_pos, end_pos: INTEGER_32): STRING_8
			-- Copy of substring containing all characters at indices
			-- between start_pos and end_pos.
		require
			start_position_big_enough: start_pos >= 1
			end_position_big_enough: start_pos <= end_pos + 1
			end_position_not_too_big: end_pos <= capacity
		local
			l_count: INTEGER_32
		do
			l_count := end_pos - start_pos + 1
			create Result.make (l_count);
			Result.set_count (l_count)
			read_substring_into (Result, start_pos, end_pos)
		ensure
			susbstring_not_void: Result /= Void
		end

	substring_8 (start_pos, end_pos: INTEGER_32): STRING_8
			-- Copy of substring containing all characters at indices
			-- between start_pos and end_pos.
		require
			start_position_big_enough: start_pos >= 1
			end_position_big_enough: start_pos <= end_pos + 1
			end_position_not_too_big: end_pos <= capacity
		local
			l_count: INTEGER_32
		do
			l_count := end_pos - start_pos + 1
			create Result.make (l_count);
			Result.set_count (l_count)
			read_substring_into (Result, start_pos, end_pos)
		ensure
			susbstring_not_void: Result /= Void
		end
	
feature -- Measurement

	bytes_count: INTEGER_32
			-- Number of bytes represented by the string.
		do
			Result := count
		end

	capacity: INTEGER_32
			-- Number of characters in Current.
		do
			Result := managed_data.count
		end

	Character_size: INTEGER_32 = 1
			-- Size of a character

	count: INTEGER_32
			-- Number of characters in Current.
	
feature -- Comparison

	frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_deep_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			shallow_implies_deep: standard_equal (a, b) implies Result
			both_or_none_void: (a = Void) implies (Result = (b = Void))
			same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b))
			symmetric: Result implies deep_equal (b, a)
		end

	frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b))
		end

	frozen is_deep_equal alias "≡≡≡" (other: C_STRING): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			shallow_implies_deep: standard_is_equal (other) implies Result
			same_type: Result implies same_type (other)
			symmetric: Result implies other.is_deep_equal (Current)
		end

	is_equal (other: C_STRING): BOOLEAN
			-- Is other attached to an object considered
			-- equal to current object?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		end

	frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.standard_is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b))
		end

	frozen standard_is_equal alias "" (other: C_STRING): BOOLEAN
			-- Is other attached to an object of the same type
			-- as current object, and field-by-field identical to it?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)
		end
	
feature -- Status report

	conforms_to (other: ANY): BOOLEAN
			-- Does type of current object conform to type
			-- of other (as per Eiffel: The Language, chapter 13)?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		end

	same_type (other: ANY): BOOLEAN
			-- Is type of current object identical to type of other?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
		end
	
feature -- Element change

	fill_blank
			-- Fill Current with zeros.
		do
			fill_value (0)
		end

	fill_value (a_value: INTEGER_8)
			-- Fill Current with a_value.
		do
			managed_data.item.memory_set (a_value.to_integer_32, managed_data.count)
		end

	set_count (a_count: INTEGER_32)
			-- Set count with a_count.
			-- Note: Current content from index 1 to
			-- count.min (a_count) is unchanged.
		require
			a_count_non_negative: a_count >= 0
		local
			new_size: INTEGER_32
		do
			new_size := a_count + 1
			if managed_data.count < new_size then
				managed_data.resize (new_size)
			end
			count := a_count
		ensure
			count_set: count = a_count
		end

	set_string (a_string: READABLE_STRING_GENERAL)
			-- Set string with a_string.
		require
			a_string_not_void: a_string /= Void
			a_string_is_string_8: a_string.is_valid_as_string_8
		do
			set_substring (a_string, 1, a_string.count)
		end

	set_substring (a_string: READABLE_STRING_GENERAL; start_pos, end_pos: INTEGER_32)
			-- Set string with a_string.
		require
			a_string_not_void: a_string /= Void
			start_position_big_enough: start_pos >= 1
			end_position_big_enough: start_pos <= end_pos + 1
			end_pos_small_enough: end_pos <= a_string.count
		local
			i, nb: INTEGER_32
			new_size: INTEGER_32
		do
			nb := end_pos - start_pos + 1
			count := nb
			new_size := nb + 1
			if managed_data.count < new_size then
				managed_data.resize (new_size)
			end
			from
				i := 0
			until
				i = nb
			loop
				managed_data.put_natural_8 (a_string.code (i + start_pos).to_natural_8, i)
				i := i + 1
			end;
			managed_data.put_natural_8 (0, nb)
		end
	
feature -- Duplication

	frozen clone (other: detachable ANY): like other
		obsolete "Use `twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- equal to other
			--
			-- For non-void other, clone calls copy;
			-- to change copying/cloning semantics, redefine copy.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.twin
			end
		ensure -- from ANY
			instance_free: class
			equal: Result ~ other
		end

	copy (other: C_STRING)
			-- Update current object using fields of object attached
			-- to other, so as to yield equal objects.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_equal: Current ~ other
		end

	frozen deep_clone (other: detachable ANY): like other
		obsolete "Use `deep_twin' instead. [2017-05-31]"
			-- Void if other is void: otherwise, new object structure
			-- recursively duplicated from the one attached to other
			-- (from ANY)
		do
			if other /= Void then
				Result := other.deep_twin
			end
		ensure -- from ANY
			instance_free: class
			deep_equal: deep_equal (other, Result)
		end

	frozen deep_copy (other: C_STRING)
			-- Effect equivalent to that of:
			--		copy (other . deep_twin)
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		do
			copy (other.deep_twin)
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)
		end

	frozen deep_twin: C_STRING
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)
		end

	frozen standard_clone (other: detachable ANY): like other
		obsolete "Use `standard_twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.standard_twin
			end
		ensure -- from ANY
			instance_free: class
			equal: standard_equal (Result, other)
		end

	frozen standard_copy (other: C_STRING)
			-- Copy every field of other onto corresponding field
			-- of current object.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)
		end

	frozen standard_twin: C_STRING
			-- New object field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			standard_twin_not_void: Result /= Void
			equal: standard_equal (Result, Current)
		end

	frozen twin: C_STRING
			-- New object equal to Current
			-- twin calls copy; to change copying/twinning semantics, redefine copy.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result ~ Current
		end
	
feature -- Basic operations

	frozen as_attached: attached C_STRING
		obsolete "Remove calls to this feature. [2017-05-31]"
			-- Attached version of Current.
			-- (Can be used during transitional period to convert
			-- non-void-safe classes to void-safe ones.)
			-- (from ANY)
		do
			Result := Current
		end

	frozen default: detachable C_STRING
			-- Default value of object's type
			-- (from ANY)
		do
		end

	frozen default_pointer: POINTER
			-- Default value of type POINTER
			-- (Avoid the need to write p.default for
			-- some p of type POINTER.)
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end
	
feature {NONE} -- Implementation

	c_strlen (ptr: POINTER): INTEGER_32
		external
			"C signature (char *): EIF_INTEGER use <string.h>"
		alias
			"strlen"
		end
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		once
			create Result;
			Result.set_output_default
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void
		end

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		do
			Result := tagged_out
		ensure -- from ANY
			out_not_void: Result /= Void
		end

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		local
			s: READABLE_STRING_8
		do
			if attached o then
				s := o.out
				if attached {READABLE_STRING_32} s as s32 then
					Io.put_string_32 (s32)
				elseif attached {READABLE_STRING_8} s as s8 then
					Io.put_string (s8)
				else
					Io.put_string_32 (s.as_string_32)
				end
			end
		ensure -- from ANY
			instance_free: class
		end

	frozen tagged_out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			tagged_out_not_void: Result /= Void
		end
	
feature -- Platform

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		once
			create Result
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
		end
	
feature {NONE} -- Retrieval

	frozen internal_correct_mismatch
			-- Called from runtime to perform a proper dynamic dispatch on correct_mismatch
			-- from MISMATCH_CORRECTOR.
			-- (from ANY)
		local
			l_msg: STRING_32
			l_exc: EXCEPTIONS
		do
			if attached {MISMATCH_CORRECTOR} Current as l_corrector then
				l_corrector.correct_mismatch
			else
				create l_msg.make_from_string ("Mismatch: ".as_string_32)
				create l_exc;
				l_msg.append (generating_type.name_32);
				l_exc.raise_retrieval_exception (l_msg)
			end
		end
	
invariant
	managed_data_not_void: managed_data /= Void
	count_not_negative: count >= 0

		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

note
	copyright: "Copyright (c) 1984-2020, Eiffel Software and others"
	license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
	source: "[
		Eiffel Software
		5949 Hollister Ave., Goleta, CA 93117 USA
		Telephone 805-685-1006, Fax 805-685-6869
		Website http://www.eiffel.com
		Customer support http://support.eiffel.com
	]"

end -- class C_STRING

Generated by ISE EiffelStudio