indexing
	description: "[
		Sequences of characters, accessible through integer indices
		in a contiguous range.
	]"
	library: "Free implementation of ELKS library"
	copyright: "Copyright (c) 1986-2008, Eiffel Software and others"
	license: "Eiffel Forum License v2 (see forum.txt)"
	date: "$Date: 2008-05-23 17:24:07 -0700 (Fri, 23 May 2008) $"
	revision: "$Revision: 170 $"

class interface
	STRING_8


create 
	make (n: INTEGER_32)
			-- Allocate space for at least `n' characters.
		require
			non_negative_size: n >= 0
		ensure
			empty_string: count = 0
			area_allocated: capacity >= n

	make_empty
			-- Create empty string.
		ensure
			empty: count = 0
			area_allocated: capacity >= 0

	make_filled (c: CHARACTER_8; n: INTEGER_32)
			-- Create string of length `n' filled with `c'.
		require
			valid_count: n >= 0
		ensure
			count_set: count = n
			area_allocated: capacity >= n
			filled: occurrences (c) = count

	make_from_string (s: STRING_8)
			-- Initialize from the characters of `s'.
			-- (Useful in proper descendants of class STRING,
			-- to initialize a string-like object from a manifest string.)
		require
			string_exists: s /= Void
		ensure
			not_shared_implementation: Current /= s implies not shared_with (s)
			initialized: same_string (s)

	make_from_c (c_string: POINTER)
			-- Initialize from contents of `c_string',
			-- a string created by some C function
		require
			c_string_exists: c_string /= default_pointer

	make_from_cil (a_system_string: SYSTEM_STRING)
			-- Initialize Current with `a_system_string'.
		require
			is_dotnet: {PLATFORM}.is_dotnet

convert
	make_from_cil: {SYSTEM_STRING},
	make_from_cil: {STRING_32},
	make_from_cil ({SYSTEM_STRING})

feature -- Initialization

	adapt (s: STRING_8): like Current
			-- Object of a type conforming to the type of `s',
			-- initialized with attributes from `s'
		ensure
			adapt_not_void: Result /= Void
			shared_implementation: Result.shared_with (s)

	from_c (c_string: POINTER)
			-- Reset contents of string from contents of `c_string',
			-- a string created by some C function.
		require
			c_string_exists: c_string /= default_pointer
		ensure
			no_zero_byte: not has ('%U')

	from_c_substring (c_string: POINTER; start_pos, end_pos: INTEGER_32)
			-- Reset contents of string from substring of `c_string',
			-- a string created by some C function.
		require
			c_string_exists: c_string /= default_pointer
			start_position_big_enough: start_pos >= 1
			end_position_big_enough: start_pos <= end_pos + 1
		ensure
			valid_count: count = end_pos - start_pos + 1

	make (n: INTEGER_32)
			-- Allocate space for at least `n' characters.
		require
			non_negative_size: n >= 0
		ensure
			empty_string: count = 0
			area_allocated: capacity >= n

	make_empty
			-- Create empty string.
		ensure
			empty: count = 0
			area_allocated: capacity >= 0

	make_filled (c: CHARACTER_8; n: INTEGER_32)
			-- Create string of length `n' filled with `c'.
		require
			valid_count: n >= 0
		ensure
			count_set: count = n
			area_allocated: capacity >= n
			filled: occurrences (c) = count

	make_from_c (c_string: POINTER)
			-- Initialize from contents of `c_string',
			-- a string created by some C function
		require
			c_string_exists: c_string /= default_pointer

	make_from_cil (a_system_string: SYSTEM_STRING)
			-- Initialize Current with `a_system_string'.
		require
			is_dotnet: {PLATFORM}.is_dotnet

	make_from_string (s: STRING_8)
			-- Initialize from the characters of `s'.
			-- (Useful in proper descendants of class STRING,
			-- to initialize a string-like object from a manifest string.)
		require
			string_exists: s /= Void
		ensure
			not_shared_implementation: Current /= s implies not shared_with (s)
			initialized: same_string (s)
	
feature -- Access

	area: SPECIAL [CHARACTER_8]
			-- Special data zone
			-- (from TO_SPECIAL)

	code (i: INTEGER_32): NATURAL_32
			-- Numeric code of character at position `i'
		require -- from STRING_GENERAL
			valid_index: valid_index (i)

	false_constant: STRING_8 is "false"
			-- Constant string "false"

	fuzzy_index (other: STRING_8; start: INTEGER_32; fuzz: INTEGER_32): INTEGER_32
			-- Position of first occurrence of `other' at or after `start'
			-- with 0..`fuzz' mismatches between the string and `other'.
			-- 0 if there are no fuzzy matches
		require
			other_exists: other /= Void
			other_not_empty: not other.is_empty
			start_large_enough: start >= 1
			start_small_enough: start <= count
			acceptable_fuzzy: fuzz <= other.count

	generating_type: STRING_8
			-- Name of current object's generating type
			-- (type of which it is a direct instance)
			-- (from ANY)
		ensure -- from ANY
			generating_type_not_void: Result /= Void
			generating_type_not_empty: not Result.is_empty

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

	hash_code: INTEGER_32
			-- Hash code value
		ensure -- from HASHABLE
			good_hash_value: Result >= 0

	index_of (c: CHARACTER_8; start_index: INTEGER_32): INTEGER_32
			-- Position of first occurrence of `c' at or after `start_index';
			-- 0 if none.
		require
			start_large_enough: start_index >= 1
			start_small_enough: start_index <= count + 1
		ensure
			valid_result: Result = 0 or (start_index <= Result and Result <= count)
			zero_if_absent: (Result = 0) = not substring (start_index, count).has (c)
			found_if_present: substring (start_index, count).has (c) implies item (Result) = c
			none_before: substring (start_index, count).has (c) implies not substring (start_index, Result - 1).has (c)

	index_of_code (c: like code; start_index: INTEGER_32): INTEGER_32
			-- Position of first occurrence of `c' at or after `start_index';
			-- 0 if none.
			-- (from STRING_GENERAL)
		require -- from STRING_GENERAL
			start_large_enough: start_index >= 1
			start_small_enough: start_index <= count + 1
		ensure -- from STRING_GENERAL
			valid_result: Result = 0 or (start_index <= Result and Result <= count)
			zero_if_absent: (Result = 0) = not substring (start_index, count).has_code (c)
			found_if_present: substring (start_index, count).has_code (c) implies code (Result) = c
			none_before: substring (start_index, count).has_code (c) implies not substring (start_index, Result - 1).has_code (c)

	item (i: INTEGER_32): CHARACTER_8 assign put
			-- Character at position `i'
			-- Was declared in STRING_8 as synonym of infix "@".
		require -- from TABLE
			valid_key: valid_index (i)
		require -- from TO_SPECIAL
			valid_index: valid_index (i)

	item_code (i: INTEGER_32): INTEGER_32
			-- Numeric code of character at position `i'
		require
			index_small_enough: i <= count
			index_large_enough: i > 0

	last_index_of (c: CHARACTER_8; start_index_from_end: INTEGER_32): INTEGER_32
			-- Position of last occurrence of `c'.
			-- 0 if none
		require
			start_index_small_enough: start_index_from_end <= count
			start_index_large_enough: start_index_from_end >= 1
		ensure
			last_index_of_non_negative: Result >= 0
			correct_place: Result > 0 implies item (Result) = c

	shared_with (other: STRING_8): BOOLEAN
			-- Does string share the text of `other'?

	string: STRING_8
			-- New STRING_8 having same character sequence as `Current'.
		ensure
			string_not_void: Result /= Void
			string_type: Result.same_type (create {STRING_8}.make_empty)
			first_item: count > 0 implies Result.item (1) = item (1)
			recurse: count > 1 implies Result.substring (2, count).is_equal (substring (2, count).string)

	string_representation: STRING_8
			-- Similar to string but only create a new object if `Current' is not of dynamic type STRING_8
		ensure
			result_not_void: Result /= Void
			correct_type: Result.same_type (create {STRING_8}.make_empty)
			first_item: count > 0 implies Result.item (1) = item (1)
			recurse: count > 1 implies Result.substring (2, count).is_equal (substring (2, count).string)

	substring_index (other: STRING_8; start_index: INTEGER_32): INTEGER_32
			-- Index of first occurrence of other at or after start_index;
			-- 0 if none
		require
			other_not_void: other /= Void
			valid_start_index: start_index >= 1 and start_index <= count + 1
		ensure
			valid_result: Result = 0 or else (start_index <= Result and Result <= count - other.count + 1)
			zero_if_absent: (Result = 0) = not substring (start_index, count).has_substring (other)
			at_this_index: Result >= start_index implies other.same_string (substring (Result, Result + other.count - 1))
			none_before: Result > start_index implies not substring (start_index, Result + other.count - 2).has_substring (other)

	substring_index_in_bounds (other: STRING_8; start_pos, end_pos: INTEGER_32): INTEGER_32
			-- Position of first occurrence of `other' at or after `start_pos'
			-- and to or before `end_pos';
			-- 0 if none.
		require
			other_nonvoid: other /= Void
			other_notempty: not other.is_empty
			start_pos_large_enough: start_pos >= 1
			start_pos_small_enough: start_pos <= count
			end_pos_large_enough: end_pos >= start_pos
			end_pos_small_enough: end_pos <= count
		ensure
			correct_place: Result > 0 implies other.is_equal (substring (Result, Result + other.count - 1))

	true_constant: STRING_8 is "true"
			-- Constant string "true"

	infix "@" (i: INTEGER_32): CHARACTER_8 assign put
			-- Character at position `i'
			-- Was declared in STRING_8 as synonym of item.
		require -- from TABLE
			valid_key: valid_index (i)
		require -- from TO_SPECIAL
			valid_index: valid_index (i)
	
feature -- Measurement

	additional_space: INTEGER_32
			-- Proposed number of additional items
			-- (from RESIZABLE)
		ensure -- from RESIZABLE
			at_least_one: Result >= 1

	capacity: INTEGER_32
			-- Allocated space
		require -- from  STRING_GENERAL
			True
		require -- from  BOUNDED
			True
		ensure -- from STRING_GENERAL
			capacity_non_negative: Result >= 0

	count: INTEGER_32
			-- Actual number of characters making up the string

	growth_percentage: INTEGER_32 is 50
			-- Percentage by which structure will grow automatically
			-- (from RESIZABLE)

	index_set: INTEGER_INTERVAL
			-- Range of acceptable indexes
		require -- from  INDEXABLE
			True
		ensure -- from INDEXABLE
			not_void: Result /= Void
		ensure then
			Result.count = count

	minimal_increase: INTEGER_32 is 5
			-- Minimal number of additional items
			-- (from RESIZABLE)

	occurrences (c: CHARACTER_8): INTEGER_32
			-- Number of times `c' appears in the string
		require -- from  BAG
			True
		ensure -- from BAG
			non_negative_occurrences: Result >= 0
		ensure then
			zero_if_empty: count = 0 implies Result = 0
			recurse_if_not_found_at_first_position: (count > 0 and then item (1) /= c) implies Result = substring (2, count).occurrences (c)
			recurse_if_found_at_first_position: (count > 0 and then item (1) = c) implies Result = 1 + substring (2, count).occurrences (c)
	
feature -- Comparison

	frozen deep_equal (some: ?ANY; other: like arg #1): BOOLEAN
			-- Are `some' and `other' either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		ensure -- from ANY
			shallow_implies_deep: standard_equal (some, other) implies Result
			both_or_none_void: (some = Void) implies (Result = (other = Void))
			same_type: (Result and (some /= Void)) implies (other /= Void and then some.same_type (other))
			symmetric: Result implies deep_equal (other, some)

	frozen equal (some: ?ANY; other: like arg #1): BOOLEAN
			-- Are `some' and `other' either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		ensure -- from ANY
			definition: Result = (some = Void and other = Void) or else ((some /= Void and other /= Void) and then some.is_equal (other))

	is_case_insensitive_equal (other: like Current): BOOLEAN
			-- Is string made of same character sequence as `other' regardless of casing
			-- (possibly with a different capacity)?
		require
			other_not_void: other /= Void
		ensure
			symmetric: Result implies other.is_case_insensitive_equal (Current)
			consistent: standard_is_equal (other) implies Result
			valid_result: as_lower.is_equal (other.as_lower) implies Result

	frozen is_deep_equal (other: STRING_8): BOOLEAN
			-- Are `Current' and `other' attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		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)

	is_equal (other: like Current): BOOLEAN
			-- Is string made of same character sequence as `other'
			-- (possibly with a different capacity)?
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			symmetric: Result implies other.is_equal (Current)
			consistent: standard_is_equal (other) implies Result
		ensure then -- from COMPARABLE
			trichotomy: Result = (not (Current < other) and not (other < Current))

	max (other: STRING_8): STRING_8
			-- The greater of current object and `other'
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		ensure -- from COMPARABLE
			current_if_not_smaller: Current >= other implies Result = Current
			other_if_smaller: Current < other implies Result = other

	min (other: STRING_8): STRING_8
			-- The smaller of current object and `other'
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		ensure -- from COMPARABLE
			current_if_not_greater: Current <= other implies Result = Current
			other_if_greater: Current > other implies Result = other

	same_string (other: STRING_8): BOOLEAN
			-- Do `Current' and `other' have same character sequence?
		require
			other_not_void: other /= Void
		ensure
			definition: Result = string.is_equal (other.string)

	frozen standard_equal (some: ?ANY; other: like arg #1): BOOLEAN
			-- Are `some' and `other' either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		ensure -- from ANY
			definition: Result = (some = Void and other = Void) or else ((some /= Void and other /= Void) and then some.standard_is_equal (other))

	frozen standard_is_equal (other: STRING_8): 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
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)

	three_way_comparison (other: STRING_8): INTEGER_32
			-- If current object equal to `other', 0;
			-- if smaller, -1; if greater, 1
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		ensure -- from COMPARABLE
			equal_zero: (Result = 0) = is_equal (other)
			smaller_negative: (Result = -1) = (Current < other)
			greater_positive: (Result = 1) = (Current > other)

	infix "<" (other: like Current): BOOLEAN
			-- Is string lexicographically lower than `other'?
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			asymmetric: Result implies not (other < Current)

	infix "<=" (other: STRING_8): BOOLEAN
			-- Is current object less than or equal to `other'?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			definition: Result = ((Current < other) or is_equal (other))

	infix ">" (other: STRING_8): BOOLEAN
			-- Is current object greater than `other'?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			definition: Result = (other < Current)

	infix ">=" (other: STRING_8): BOOLEAN
			-- Is current object greater than or equal to `other'?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			definition: Result = (other <= Current)
	
feature -- Status report

	changeable_comparison_criterion: BOOLEAN is False

	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

	ends_with (s: STRING_8): BOOLEAN
			-- Does string finish with `s'?
		require
			argument_not_void: s /= Void
		ensure
			definition: Result = s.same_string (substring (count - s.count + 1, count))

	extendible: BOOLEAN is True
			-- May new items be added? (Answer: yes.)

	full: BOOLEAN
			-- Is structure full?
			-- (from BOUNDED)

	has (c: CHARACTER_8): BOOLEAN
			-- Does string include `c'?
		require -- from  CONTAINER
			True
		ensure -- from CONTAINER
			not_found_in_empty: Result implies not is_empty
		ensure then
			false_if_empty: count = 0 implies not Result
			true_if_first: count > 0 and then item (1) = c implies Result
			recurse: (count > 0 and then item (1) /= c) implies (Result = substring (2, count).has (c))

	has_code (c: like code): BOOLEAN
			-- Does string include `c'?
			-- (from STRING_GENERAL)
		ensure then -- from STRING_GENERAL
			false_if_empty: count = 0 implies not Result
			true_if_first: count > 0 and then code (1) = c implies Result
			recurse: (count > 0 and then code (1) /= c) implies (Result = substring (2, count).has_code (c))

	has_substring (other: STRING_8): BOOLEAN
			-- Does `Current' contain `other'?
		require
			other_not_void: other /= Void
		ensure
			false_if_too_small: count < other.count implies not Result
			true_if_initial: (count