Skip to main content

A.4 String Handling

danger

This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue

1/3

This subclause presents the specifications of the package Strings and several child packages, which provide facilities for dealing with string data. Fixed-length, bounded-length, and unbounded-length strings are supported, for String, Wide_String, and Wide_Wide_String. The string-handling subprograms include searches for pattern strings and for characters in program-specified sets, translation (via a character-to-character mapping), and transformation (replacing, inserting, overwriting, and deleting of substrings).

Extensions to Ada 83

1.a/3

This subclause is new to Ada 95.

Wording Changes from Ada 95

1.b/2

Included Wide_Wide_String in this description; the individual changes are documented as extensions as needed.

A.4.1 The Package Strings

1

The package Strings provides declarations common to the string handling packages.

Static Semantics

2

The library package Strings has the following declaration:

3/5

package Ada.Strings with Pure is 4/2

Space : constant Character := ' '; Wide_Space : constant Wide_Character := ' '; Wide_Wide_Space : constant Wide_Wide_Character := ' '; 5 Length_Error, Pattern_Error, Index_Error, Translation_Error : exception; 6 type Alignment is (Left, Right, Center); type Truncation is (Left, Right, Error); type Membership is (Inside, Outside); type Direction is (Forward, Backward); type Trim_End is (Left, Right, Both); end Ada.Strings;

Incompatibilities With Ada 95

6.a/3

Constant Wide_Wide_Space is added to Ada.Strings. If Ada.Strings is referenced in a use_clause, and an entity E with a defining_identifier of Wide_Wide_Space is defined in a package that is also referenced in a use_clause, the entity E may no longer be use-visible, resulting in errors. This should be rare and is easily fixed if it does occur.

A.4.2 The Package Strings.Maps

1/5

The package Strings.Maps defines the types, operations, and other entities necessary for character sets and character-to-character mappings.

Static Semantics

2

The library package Strings.Maps has the following declaration:

3/5

package Ada.Strings.Maps with Pure is 4/5

-- Representation for a set of character values: type Character_Set is private with Preelaborable_Initialization; 5 Null_Set : constant Character_Set; 6 type Character_Range is record Low : Character; High : Character; end record; -- Represents Character range Low..High 7 type Character_Ranges is array (Positive range <>) of Character_Range; 8 function To_Set (Ranges : in Character_Ranges)return Character_Set; 9 function To_Set (Span : in Character_Range)return Character_Set; 10 function To_Ranges (Set : in Character_Set) return Character_Ranges; 11 function "=" (Left, Right : in Character_Set) return Boolean; 12 function "not" (Right : in Character_Set) return Character_Set; function "and" (Left, Right : in Character_Set) return Character_Set; function "or" (Left, Right : in Character_Set) return Character_Set; function "xor" (Left, Right : in Character_Set) return Character_Set; function "-" (Left, Right : in Character_Set) return Character_Set; 13 function Is_In (Element : in Character; Set : in Character_Set) return Boolean; 14 function Is_Subset (Elements : in Character_Set; Set : in Character_Set) return Boolean; 15 function "<=" (Left : in Character_Set; Right : in Character_Set) return Boolean renames Is_Subset; 16 -- Alternative representation for a set of character values: subtype Character_Sequence is String; 17 function To_Set (Sequence : in Character_Sequence)return Character_Set; 18 function To_Set (Singleton : in Character) return Character_Set; 19 function To_Sequence (Set : in Character_Set) return Character_Sequence; 20/5

-- Representation for a character to character mapping: type Character_Mapping is private with Preelaborable_Initialization; 21 function Value (Map : in Character_Mapping; Element : in Character) return Character; 22 Identity : constant Character_Mapping; 23 function To_Mapping (From, To : in Character_Sequence) return Character_Mapping; 24 function To_Domain (Map : in Character_Mapping) return Character_Sequence; function To_Range (Map : in Character_Mapping) return Character_Sequence; 25 type Character_Mapping_Function is access function (From : in Character) return Character; 26 private ... -- not specified by the language end Ada.Strings.Maps;

27

An object of type Character_Set represents a set of characters.

28

Null_Set represents the set containing no characters.

29

An object Obj of type Character_Range represents the set of characters in the range Obj.Low .. Obj.High.

30

An object Obj of type Character_Ranges represents the union of the sets corresponding to Obj(I) for I in Obj'Range.

31

function To_Set (Ranges : in Character_Ranges) return Character_Set;

32/3

If Ranges'Length=0 then Null_Set is returned; otherwise, the returned value represents the set corresponding to Ranges.

33

function To_Set (Span : in Character_Range) return Character_Set;

34

The returned value represents the set containing each character in Span.

35

function To_Ranges (Set : in Character_Set) return Character_Ranges;

36/3

If Set = Null_Set, then an empty Character_Ranges array is returned; otherwise, the shortest array of contiguous ranges of Character values in Set, in increasing order of Low, is returned.

37

function "=" (Left, Right : in Character_Set) return Boolean;

38

The function "=" returns True if Left and Right represent identical sets, and False otherwise.

39

Each of the logical operators "not", "and", "or", and "xor" returns a Character_Set value that represents the set obtained by applying the corresponding operation to the set(s) represented by the parameter(s) of the operator. "–"(Left, Right) is equivalent to "and"(Left, "not"(Right)).

39.a
reason

The set minus operator is provided for efficiency.

40

function Is_In (Element : in Character; Set : in Character_Set); return Boolean;

41

Is_In returns True if Element is in Set, and False otherwise.

42

function Is_Subset (Elements : in Character_Set; Set : in Character_Set) return Boolean;

43

Is_Subset returns True if Elements is a subset of Set, and False otherwise.

44

subtype Character_Sequence is String;

45

The Character_Sequence subtype is used to portray a set of character values and also to identify the domain and range of a character mapping.

45.a
reason

Although a named subtype is redundant — the predefined type String could have been used for the parameter to To_Set and To_Mapping below — the use of a differently named subtype identifies the intended purpose of the parameter.

46

function To_Set (Sequence : in Character_Sequence) return Character_Set; function To_Set (Singleton : in Character) return Character_Set;

47

Sequence portrays the set of character values that it explicitly contains (ignoring duplicates). Singleton portrays the set comprising a single Character. Each of the To_Set functions returns a Character_Set value that represents the set portrayed by Sequence or Singleton.

48

function To_Sequence (Set : in Character_Set) return Character_Sequence;

49

The function To_Sequence returns a Character_Sequence value containing each of the characters in the set represented by Set, in ascending order with no duplicates.

50

type Character_Mapping is private;

51

An object of type Character_Mapping represents a Character-to-Character mapping.

52

function Value (Map : in Character_Mapping; Element : in Character) return Character;

53

The function Value returns the Character value to which Element maps with respect to the mapping represented by Map.

54

A character C matches a pattern character P with respect to a given Character_Mapping value Map if Value(Map, C) = P. A string S matches a pattern string P with respect to a given Character_Mapping if their lengths are the same and if each character in S matches its corresponding character in the pattern string P.

54.a
discussion

In an earlier version of the string handling packages, the definition of matching was symmetrical, namely C matches P if Value(Map,C) = Value(Map,P). However, applying the mapping to the pattern was confusing according to some reviewers. Furthermore, if the symmetrical version is needed, it can be achieved by applying the mapping to the pattern (via translation) prior to passing it as a parameter.

55

String handling subprograms that deal with character mappings have parameters whose type is Character_Mapping.

56

Identity : constant Character_Mapping;

57

Identity maps each Character to itself.

58

function To_Mapping (From, To : in Character_Sequence) return Character_Mapping;

59

To_Mapping produces a Character_Mapping such that each element of From maps to the corresponding element of To, and each other character maps to itself. If From'Length /= To'Length, or if some character is repeated in From, then Translation_Error is propagated.

60

function To_Domain (Map : in Character_Mapping) return Character_Sequence;

61

To_Domain returns the shortest Character_Sequence value D such that each character not in D maps to itself, and such that the characters in D are in ascending order. The lower bound of D is 1.

62

function To_Range (Map : in Character_Mapping) return Character_Sequence;

63/1

{8652/0048} To_Range returns the Character_Sequence value R, such that if D = To_Domain(Map), then R has the same bounds as D, and D(I) maps to R(I) for each I in D'Range.

64

An object F of type Character_Mapping_Function maps a Character value C to the Character value F.all(C), which is said to match C with respect to mapping function F.

65

NOTE 1 Character_Mapping and Character_Mapping_Function are used both for character equivalence mappings in the search subprograms (such as for case insensitivity) and as transformational mappings in the Translate subprograms.

66

NOTE 2 To_Domain(Identity) and To_Range(Identity) each returns the null string.

66.a
reason

Package Strings.Maps is not pure, since it declares an access-to-subprogram type.

Examples

67/5

Example of use of Strings.Maps.To_Mapping:

68

To_Mapping("ABCD", "ZZAB") returns a Character_Mapping that maps 'A' and 'B' to 'Z', 'C' to 'A', 'D' to 'B', and each other Character to itself.

Extensions to Ada 95

68.a/2
correction

Amendment Added pragma Preelaborable_Initialization to types Character_Set and Character_Mapping, so that they can be used to declare default-initialized objects in preelaborated units.

68.b/2

Strings.Maps is now Pure, so it can be used in pure units.

Wording Changes from Ada 95

68.c/2

{8652/0048} Corrigendum: Corrected the definition of the range of the result of To_Range, since the Ada 95 definition makes no sense.

A.4.3 Fixed-Length String Handling

1

The language-defined package Strings.Fixed provides string-handling subprograms for fixed-length strings; that is, for values of type Standard.String. Several of these subprograms are procedures that modify the contents of a String that is passed as an out or an in out parameter; each has additional parameters to control the effect when the logical length of the result differs from the parameter's length.

2

For each function that returns a String, the lower bound of the returned value is 1.

2.a/2
discussion

Most operations that yield a String are provided both as a function and as a procedure. The functional form is possibly a more aesthetic style but may introduce overhead due to extra copying or dynamic memory usage in some implementations. Thus a procedural form, with an in out parameter so that all copying is done `in place', is also supplied.

3

The basic model embodied in the package is that a fixed-length string comprises significant characters and possibly padding (with space characters) on either or both ends. When a shorter string is copied to a longer string, padding is inserted, and when a longer string is copied to a shorter one, padding is stripped. The Move procedure in Strings.Fixed, which takes a String as an out parameter, allows the programmer to control these effects. Similar control is provided by the string transformation procedures.

Static Semantics

4

The library package Strings.Fixed has the following declaration:

5/5

with Ada.Strings.Maps; package Ada.Strings.Fixed with Preelaborate, Nonblocking, Global => in out synchronized is 6 -- "Copy" procedure for strings of possibly different lengths 7 procedure Move (Source : in String; Target : out String; Drop : in Truncation := Error; Justify : in Alignment := Left; Pad : in Character := Space); 8 -- Search subprograms 8.1/2

function Index (Source : in String; Pattern : in String; From : in Positive; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; 8.2/2

function Index (Source : in String; Pattern : in String; From : in Positive; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping_Function) return Natural; 9 function Index (Source : in String; Pattern : in String; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; 10 function Index (Source : in String; Pattern : in String; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping_Function) return Natural; 10.1/2

function Index (Source : in String; Set : in Maps.Character_Set; From : in Positive; Test : in Membership := Inside; Going : in Direction := Forward) return Natural; 11 function Index (Source : in String; Set : in Maps.Character_Set; Test : in Membership := Inside; Going : in Direction := Forward) return Natural; 11.1/2

function Index_Non_Blank (Source : in String; From : in Positive; Going : in Direction := Forward) return Natural; 12 function Index_Non_Blank (Source : in String; Going : in Direction := Forward) return Natural; 13 function Count (Source : in String; Pattern : in String; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; 14 function Count (Source : in String; Pattern : in String; Mapping : in Maps.Character_Mapping_Function) return Natural; 15 function Count (Source : in String; Set : in Maps.Character_Set) return Natural; 15.1/3

procedure Find_Token (Source : in String; Set : in Maps.Character_Set; From : in Positive; Test : in Membership; First : out Positive; Last : out Natural); 16 procedure Find_Token (Source : in String; Set : in Maps.Character_Set; Test : in Membership; First : out Positive; Last : out Natural); 17 -- String translation subprograms 18 function Translate (Source : in String; Mapping : in Maps.Character_Mapping) return String; 19 procedure Translate (Source : in out String; Mapping : in Maps.Character_Mapping); 20 function Translate (Source : in String; Mapping : in Maps.Character_Mapping_Function) return String; 21 procedure Translate (Source : in out String; Mapping : in Maps.Character_Mapping_Function); 22 -- String transformation subprograms 23 function Replace_Slice (Source : in String; Low : in Positive; High : in Natural; By : in String) return String; 24 procedure Replace_Slice (Source : in out String; Low : in Positive; High : in Natural; By : in String; Drop : in Truncation := Error; Justify : in Alignment := Left; Pad : in Character := Space); 25 function Insert (Source : in String; Before : in Positive; New_Item : in String) return String; 26 procedure Insert (Source : in out String; Before : in Positive; New_Item : in String; Drop : in Truncation := Error); 27 function Overwrite (Source : in String; Position : in Positive; New_Item : in String) return String; 28 procedure Overwrite (Source : in out String; Position : in Positive; New_Item : in String; Drop : in Truncation := Right); 29 function Delete (Source : in String; From : in Positive; Through : in Natural) return String; 30 procedure Delete (Source : in out String; From : in Positive; Through : in Natural; Justify : in Alignment := Left; Pad : in Character := Space); 31 --String selector subprograms function Trim (Source : in String; Side : in Trim_End) return String; 32 procedure Trim (Source : in out String; Side : in Trim_End; Justify : in Alignment := Left; Pad : in Character := Space); 33 function Trim (Source : in String; Left : in Maps.Character_Set; Right : in Maps.Character_Set) return String; 34 procedure Trim (Source : in out String; Left : in Maps.Character_Set; Right : in Maps.Character_Set; Justify : in Alignment := Strings.Left; Pad : in Character := Space); 35 function Head (Source : in String; Count : in Natural; Pad : in Character := Space) return String; 36 procedure Head (Source : in out String; Count : in Natural; Justify : in Alignment := Left; Pad : in Character := Space); 37 function Tail (Source : in String; Count : in Natural; Pad : in Character := Space) return String; 38 procedure Tail (Source : in out String; Count : in Natural; Justify : in Alignment := Left; Pad : in Character := Space); 39 --String constructor functions 40 function "*" (Left : in Natural; Right : in Character) return String; 41 function "*" (Left : in Natural; Right : in String) return String; 42 end Ada.Strings.Fixed;

43

The effects of the above subprograms are as follows.

44

procedure Move (Source : in String; Target : out String; Drop : in Truncation := Error; Justify : in Alignment := Left; Pad : in Character := Space);

45/3

The Move procedure copies characters from Source to Target. If Source has the same length as Target, then the effect is to assign Source to Target. If Source is shorter than Target, then:

46
  • If Justify=Left, then Source is copied into the first Source'Length characters of Target.
  • 47
  • If Justify=Right, then Source is copied into the last Source'Length characters of Target.
  • 48
  • If Justify=Center, then Source is copied into the middle Source'Length characters of Target. In this case, if the difference in length between Target and Source is odd, then the extra Pad character is on the right.
  • 49
  • Pad is copied to each Target character not otherwise assigned.
50

If Source is longer than Target, then the effect is based on Drop.

51
  • If Drop=Left, then the rightmost Target'Length characters of Source are copied into Target.
  • 52
  • If Drop=Right, then the leftmost Target'Length characters of Source are copied into Target.
  • 53
  • If Drop=Error, then the effect depends on the value of the Justify parameter and also on whether any characters in Source other than Pad would fail to be copied:
54
  • If Justify=Left, and if each of the rightmost Source'Length-Target'Length characters in Source is Pad, then the leftmost Target'Length characters of Source are copied to Target.
  • 55
  • If Justify=Right, and if each of the leftmost Source'Length-Target'Length characters in Source is Pad, then the rightmost Target'Length characters of Source are copied to Target.
  • 56
  • Otherwise, Length_Error is propagated.
56.a
ramification

The Move procedure will work even if Source and Target overlap.

56.b
reason

The order of parameters (Source before Target) corresponds to the order in COBOL's MOVE verb.

56.1/2

function Index (Source : in String; Pattern : in String; From : in Positive; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; function Index (Source : in String; Pattern : in String; From : in Positive; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping_Function) return Natural;

56.2/3

Each Index function searches, starting from From, for a slice of Source, with length Pattern'Length, that matches Pattern with respect to Mapping; the parameter Going indicates the direction of the lookup. If Source is the null string, Index returns 0; otherwise, if From is not in Source'Range, then Index_Error is propagated. If Going = Forward, then Index returns the smallest index I which is greater than or equal to From such that the slice of Source starting at I matches Pattern. If Going = Backward, then Index returns the largest index I such that the slice of Source starting at I matches Pattern and has an upper bound less than or equal to From. If there is no such slice, then 0 is returned. If Pattern is the null string, then Pattern_Error is propagated.

56.c/2
discussion

There is no default parameter for From; the default value would need to depend on other parameters (the bounds of Source and the direction Going). It is better to use overloaded functions rather than a special value to represent the default.

56.d/2

There is no default value for the Mapping parameter that is a Character_Mapping_Function; if there were, a call would be ambiguous since there is also a default for the Mapping parameter that is a Character_Mapping.

56.e/3

The language does not define when the Pattern_Error check is made. (That's because many common searching implementations require a nonempty pattern) That means that the result for a call like Index ("", "") could be 0 or could raise Pattern_Error. Similarly, in the call Index ("", "", From => 2), the language does not define whether Pattern_Error or Index_Error is raised.

57

function Index (Source : in String; Pattern : in String; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; function Index (Source : in String; Pattern : in String; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping_Function) return Natural;

58/2

If Going = Forward, returns

58.1/2

Index (Source, Pattern, Source'First, Forward, Mapping);

58.2/3

otherwise, returns

58.3/2

Index (Source, Pattern, Source'Last, Backward, Mapping);

58.a/2

This paragraph was deleted.There is no default value for the Mapping parameter that is a Character_Mapping_Function; if there were, a call would be ambiguous since there is also a default for the Mapping parameter that is a Character_Mapping.

58.4/2

function Index (Source : in String; Set : in Maps.Character_Set; From : in Positive; Test : in Membership := Inside; Going : in Direction := Forward) return Natural;

58.5/3

Index searches for the first or last occurrence of any of a set of characters (when Test=Inside), or any of the complement of a set of characters (when Test=Outside). If Source is the null string, Index returns 0; otherwise, if From is not in Source'Range, then Index_Error is propagated. Otherwise, it returns the smallest index I >= From (if Going=Forward) or the largest index I <= From (if Going=Backward) such that Source(I) satisfies the Test condition with respect to Set; it returns 0 if there is no such Character in Source.

59

function Index (Source : in String; Set : in Maps.Character_Set; Test : in Membership := Inside; Going : in Direction := Forward) return Natural;

60/2

If Going = Forward, returns

60.1/2

Index (Source, Set, Source'First, Test, Forward);

60.2/3

otherwise, returns

60.3/2

Index (Source, Set, Source'Last, Test, Backward); 60.4/2 function Index_Non_Blank (Source : in String; From : in Positive; Going : in Direction := Forward) return Natural;

60.5/2

Returns Index (Source, Maps.To_Set(Space), From, Outside, Going);

61

function Index_Non_Blank (Source : in String; Going : in Direction := Forward) return Natural;

62

Returns Index(Source, Maps.To_Set(Space), Outside, Going)

63

function Count (Source : in String; Pattern : in String; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; function Count (Source : in String; Pattern : in String; Mapping : in Maps.Character_Mapping_Function) return Natural;

64

Returns the maximum number of nonoverlapping slices of Source that match Pattern with respect to Mapping. If Pattern is the null string then Pattern_Error is propagated.

64.a
reason

We say maximum number' because it is possible to slice a source string in different ways yielding different numbers of matches. For example if Source is "ABABABA" and Pattern is "ABA", then Count yields 2, although there is a partitioning of Source that yields just 1 match, for the middle slice. Saying maximum number' is equivalent to saying that the pattern match starts either at the low index or the high index position.

65

function Count (Source : in String; Set : in Maps.Character_Set) return Natural;

66

Returns the number of occurrences in Source of characters that are in Set.

66.1/3

procedure Find_Token (Source : in String; Set : in Maps.Character_Set; From : in Positive; Test : in Membership; First : out Positive; Last : out Natural);

66.2/3

If Source is not the null string and From is not in Source'Range, then Index_Error is raised. Otherwise, First is set to the index of the first character in Source(From .. Source'Last) that satisfies the Test condition. Last is set to the largest index such that all characters in Source(First .. Last) satisfy the Test condition. If no characters in Source(From .. Source'Last) satisfy the Test condition, First is set to From, and Last is set to 0.

67

procedure Find_Token (Source : in String; Set : in Maps.Character_Set; Test : in Membership; First : out Positive; Last : out Natural);

68/3

{8652/0049} Equivalent to Find_Token (Source, Set, Source'First, Test, First, Last).

68.a/3
ramification

If Source'First is not in Positive, which can only happen for an empty string, this will raise Constraint_Error.

69

function Translate (Source : in String; Mapping : in Maps.Character_Mapping) return String; function Translate (Source : in String; Mapping : in Maps.Character_Mapping_Function) return String;

70

Returns the string S whose length is Source'Length and such that S(I) is the character to which Mapping maps the corresponding element of Source, for I in 1..Source'Length.

71

procedure Translate (Source : in out String; Mapping : in Maps.Character_Mapping); procedure Translate (Source : in out String; Mapping : in Maps.Character_Mapping_Function);

72

Equivalent to Source := Translate(Source, Mapping).

73

function Replace_Slice (Source : in String; Low : in Positive; High : in Natural; By : in String) return String;

74/1

{8652/0049} If Low > Source'Last+1, or High < Source'First–1, then Index_Error is propagated. Otherwise:

74.1/1
  • {8652/0049} If High >= Low, then the returned string comprises Source(Source'First..Low–1) & By & Source(High+1..Source'Last), but with lower bound 1.
  • 74.2/1
  • {8652/0049} If High < Low, then the returned string is Insert(Source, Before=>Low, New_Item=>By).
75

procedure Replace_Slice (Source : in out String; Low : in Positive; High : in Natural; By : in String; Drop : in Truncation := Error; Justify : in Alignment := Left; Pad : in Character := Space);

76

Equivalent to Move(Replace_Slice(Source, Low, High, By), Source, Drop, Justify, Pad).

77

function Insert (Source : in String; Before : in Positive; New_Item : in String) return String;

78/3

Propagates Index_Error if Before is not in Source'First .. Source'Last+1; otherwise, returns Source(Source'First..Before–1) & New_Item & Source(Before..Source'Last), but with lower bound 1.

79

procedure Insert (Source : in out String; Before : in Positive; New_Item : in String; Drop : in Truncation := Error);

80

Equivalent to Move(Insert(Source, Before, New_Item), Source, Drop).

81

function Overwrite (Source : in String; Position : in Positive; New_Item : in String) return String;

82/3

Propagates Index_Error if Position is not in Source'First .. Source'Last+1; otherwise, returns the string obtained from Source by consecutively replacing characters starting at Position with corresponding characters from New_Item. If the end of Source is reached before the characters in New_Item are exhausted, the remaining characters from New_Item are appended to the string.

83

procedure Overwrite (Source : in out String; Position : in Positive; New_Item : in String; Drop : in Truncation := Right);

84

Equivalent to Move(Overwrite(Source, Position, New_Item), Source, Drop).

85

function Delete (Source : in String; From : in Positive; Through : in Natural) return String;

86/3

{8652/0049} If From <= Through, the returned string is Replace_Slice(Source, From, Through, ""); otherwise, it is Source with lower bound 1.

87

procedure Delete (Source : in out String; From : in Positive; Through : in Natural; Justify : in Alignment := Left; Pad : in Character := Space);

88

Equivalent to Move(Delete(Source, From, Through), Source, Justify => Justify, Pad => Pad).

89

function Trim (Source : in String; Side : in Trim_End) return String;

90

Returns the string obtained by removing from Source all leading Space characters (if Side = Left), all trailing Space characters (if Side = Right), or all leading and trailing Space characters (if Side = Both).

91

procedure Trim (Source : in out String; Side : in Trim_End; Justify : in Alignment := Left; Pad : in Character := Space);

92

Equivalent to Move(Trim(Source, Side), Source, Justify=>Justify, Pad=>Pad).

93

function Trim (Source : in String; Left : in Maps.Character_Set; Right : in Maps.Character_Set) return String;

94

Returns the string obtained by removing from Source all leading characters in Left and all trailing characters in Right.

95

procedure Trim (Source : in out String; Left : in Maps.Character_Set; Right : in Maps.Character_Set; Justify : in Alignment := Strings.Left; Pad : in Character := Space);

96

Equivalent to Move(Trim(Source, Left, Right), Source, Justify => Justify, Pad=>Pad).

97

function Head (Source : in String; Count : in Natural; Pad : in Character := Space) return String;

98/3

Returns a string of length Count. If Count <= Source'Length, the string comprises the first Count characters of Source. Otherwise, its contents are Source concatenated with Count–Source'Length Pad characters.

99

procedure Head (Source : in out String; Count : in Natural; Justify : in Alignment := Left; Pad : in Character := Space);

100

Equivalent to Move(Head(Source, Count, Pad), Source, Drop=>Error, Justify=>Justify, Pad=>Pad).

101

function Tail (Source : in String; Count : in Natural; Pad : in Character := Space) return String;

102/3

Returns a string of length Count. If Count <= Source'Length, the string comprises the last Count characters of Source. Otherwise, its contents are Count-Source'Length Pad characters concatenated with Source.

103

procedure Tail (Source : in out String; Count : in Natural; Justify : in Alignment := Left; Pad : in Character := Space);

104

Equivalent to Move(Tail(Source, Count, Pad), Source, Drop=>Error, Justify=>Justify, Pad=>Pad).

105

function "*" (Left : in Natural; Right : in Character) return String; function "*" (Left : in Natural; Right : in String) return String;

106/1

{8652/0049} These functions replicate a character or string a specified number of times. The first function returns a string whose length is Left and each of whose elements is Right. The second function returns a string whose length is Left*Right'Length and whose value is the null string if Left = 0 and otherwise is (Left–1)*Right & Right with lower bound 1.

107/5

NOTE 1 In the Index and Count functions taking Pattern and Mapping parameters, for there to be a match, the actual String parameter passed to Pattern can contain only characters occurring as target characters of the mapping.

108

NOTE 2 In the Insert subprograms, inserting at the end of a string is obtained by passing Source'Last+1 as the Before parameter.

109

NOTE 3 If a null Character_Mapping_Function is passed to any of the string handling subprograms, Constraint_Error is propagated.

Incompatibilities With Ada 95

109.a/3

Overloaded versions of Index and Index_Non_Blank are added to Strings.Fixed. If Strings.Fixed is referenced in a use_clause, and an entity E with a defining_identifier of Index or Index_Non_Blank is defined in a package that is also referenced in a use_clause, the entity E may no longer be use-visible, resulting in errors. This should be rare and is easily fixed if it does occur.

Wording Changes from Ada 95

109.b/2

{8652/0049} Corrigendum: Clarified that Find_Token may raise Constraint_Error if Source'First is not in Positive (which is only possible for a null string).

109.c/2

{8652/0049} Corrigendum: Clarified that Replace_Slice, Delete, and "*" always return a string with lower bound 1.

Incompatibilities With Ada 2005

109.d/3

An overloaded version of Find_Token is added to Strings.Fixed. If Strings.Fixed is referenced in a use_clause, and an entity E with a defining_identifier of Find_Token is defined in a package that is also referenced in a use_clause, the entity E may no longer be use-visible, resulting in errors. This should be rare and is easily fixed if it does occur.

Wording Changes from Ada 2005

109.e/3
correction

Clarified that Index never raises Index_Error if the source string is null.

A.4.4 Bounded-Length String Handling

1/5

The language-defined package Strings.Bounded provides a generic package each of whose instances yields a private type Bounded_String and a set of operations. An object of a particular Bounded_String type represents a String whose low bound is 1 and whose length can vary conceptually between 0 and a maximum size established at the generic instantiation. The subprograms for fixed-length string handling are either overloaded directly for Bounded_String, or are modified as necessary to reflect the variability in length. Additionally, since the Bounded_String type is private, appropriate constructor and selector operations are provided.

1.a
reason

Strings.Bounded declares an inner generic package, versus itself being directly a generic child of Strings, in order to retain compatibility with a version of the string-handling packages that is generic with respect to the character and string types.

1.b
reason

The bound of a bounded-length string is specified as a parameter to a generic, versus as the value for a discriminant, because of the inappropriateness of assignment and equality of discriminated types for the copying and comparison of bounded strings.

Static Semantics

2

The library package Strings.Bounded has the following declaration:

3/5

with Ada.Strings.Maps; package Ada.Strings.Bounded with Preelaborate, Nonblocking, Global => in out synchronized is 4 generic Max : Positive; -- Maximum length of a Bounded_String package Generic_Bounded_Length is 5 Max_Length : constant Positive := Max; 6 type Bounded_String is private; 7 Null_Bounded_String : constant Bounded_String; 8 subtype Length_Range is Natural range 0 .. Max_Length; 9 function Length (Source : in Bounded_String) return Length_Range; 10 -- Conversion, Concatenation, and Selection functions 11 function To_Bounded_String (Source : in String; Drop : in Truncation := Error) return Bounded_String; 12 function To_String (Source : in Bounded_String) return String; 12.1/2

procedure Set_Bounded_String (Target : out Bounded_String; Source : in String; Drop : in Truncation := Error); 13 function Append (Left, Right : in Bounded_String; Drop : in Truncation := Error) return Bounded_String; 14 function Append (Left : in Bounded_String; Right : in String; Drop : in Truncation := Error) return Bounded_String; 15 function Append (Left : in String; Right : in Bounded_String; Drop : in Truncation := Error) return Bounded_String; 16 function Append (Left : in Bounded_String; Right : in Character; Drop : in Truncation := Error) return Bounded_String; 17 function Append (Left : in Character; Right : in Bounded_String; Drop : in Truncation := Error) return Bounded_String; 18 procedure Append (Source : in out Bounded_String; New_Item : in Bounded_String; Drop : in Truncation := Error); 19 procedure Append (Source : in out Bounded_String; New_Item : in String; Drop : in Truncation := Error); 20 procedure Append (Source : in out Bounded_String; New_Item : in Character; Drop : in Truncation := Error); 21 function "&" (Left, Right : in Bounded_String) return Bounded_String; 22 function "&" (Left : in Bounded_String; Right : in String) return Bounded_String; 23 function "&" (Left : in String; Right : in Bounded_String) return Bounded_String; 24 function "&" (Left : in Bounded_String; Right : in Character) return Bounded_String; 25 function "&" (Left : in Character; Right : in Bounded_String) return Bounded_String; 26 function Element (Source : in Bounded_String; Index : in Positive) return Character; 27 procedure Replace_Element (Source : in out Bounded_String; Index : in Positive; By : in Character); 28 function Slice (Source : in Bounded_String; Low : in Positive; High : in Natural) return String; 28.1/2

function Bounded_Slice (Source : in Bounded_String; Low : in Positive; High : in Natural) return Bounded_String; 28.2/2

procedure Bounded_Slice (Source : in Bounded_String; Target : out Bounded_String; Low : in Positive; High : in Natural); 29 function "=" (Left, Right : in Bounded_String) return Boolean; function "=" (Left : in Bounded_String; Right : in String) return Boolean; 30 function "=" (Left : in String; Right : in Bounded_String) return Boolean; 31 function "<" (Left, Right : in Bounded_String) return Boolean; 32 function "<" (Left : in Bounded_String; Right : in String) return Boolean; 33 function "<" (Left : in String; Right : in Bounded_String) return Boolean; 34 function "<=" (Left, Right : in Bounded_String) return Boolean; 35 function "<=" (Left : in Bounded_String; Right : in String) return Boolean; 36 function "<=" (Left : in String; Right : in Bounded_String) return Boolean; 37 function ">" (Left, Right : in Bounded_String) return Boolean; 38 function ">" (Left : in Bounded_String; Right : in String) return Boolean; 39 function ">" (Left : in String; Right : in Bounded_String) return Boolean; 40 function ">=" (Left, Right : in Bounded_String) return Boolean; 41 function ">=" (Left : in Bounded_String; Right : in String) return Boolean; 42 function ">=" (Left : in String; Right : in Bounded_String) return Boolean; 43/2

-- Search subprograms 43.1/2

function Index (Source : in Bounded_String; Pattern : in String; From : in Positive; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; 43.2/2

function Index (Source : in Bounded_String; Pattern : in String; From : in Positive; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping_Function) return Natural; 44 function Index (Source : in Bounded_String; Pattern : in String; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; 45 function Index (Source : in Bounded_String; Pattern : in String; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping_Function) return Natural; 45.1/2

function Index (Source : in Bounded_String; Set : in Maps.Character_Set; From : in Positive; Test : in Membership := Inside; Going : in Direction := Forward) return Natural; 46 function Index (Source : in Bounded_String; Set : in Maps.Character_Set; Test : in Membership := Inside; Going : in Direction := Forward) return Natural; 46.1/2

function Index_Non_Blank (Source : in Bounded_String; From : in Positive; Going : in Direction := Forward) return Natural; 47 function Index_Non_Blank (Source : in Bounded_String; Going : in Direction := Forward) return Natural; 48 function Count (Source : in Bounded_String; Pattern : in String; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; 49 function Count (Source : in Bounded_String; Pattern : in String; Mapping : in Maps.Character_Mapping_Function) return Natural; 50 function Count (Source : in Bounded_String; Set : in Maps.Character_Set) return Natural; 50.1/3

procedure Find_Token (Source : in Bounded_String; Set : in Maps.Character_Set; From : in Positive; Test : in Membership; First : out Positive; Last : out Natural); 51 procedure Find_Token (Source : in Bounded_String; Set : in Maps.Character_Set; Test : in Membership; First : out Positive; Last : out Natural); 52 -- String translation subprograms 53 function Translate (Source : in Bounded_String; Mapping : in Maps.Character_Mapping) return Bounded_String; 54 procedure Translate (Source : in out Bounded_String; Mapping : in Maps.Character_Mapping); 55 function Translate (Source : in Bounded_String; Mapping : in Maps.Character_Mapping_Function) return Bounded_String; 56 procedure Translate (Source : in out Bounded_String; Mapping : in Maps.Character_Mapping_Function); 57 -- String transformation subprograms 58 function Replace_Slice (Source : in Bounded_String; Low : in Positive; High : in Natural; By : in String; Drop : in Truncation := Error) return Bounded_String; 59 procedure Replace_Slice (Source : in out Bounded_String; Low : in Positive; High : in Natural; By : in String; Drop : in Truncation := Error); 60 function Insert (Source : in Bounded_String; Before : in Positive; New_Item : in String; Drop : in Truncation := Error) return Bounded_String; 61 procedure Insert (Source : in out Bounded_String; Before : in Positive; New_Item : in String; Drop : in Truncation := Error); 62 function Overwrite (Source : in Bounded_String; Position : in Positive; New_Item : in String; Drop : in Truncation := Error) return Bounded_String; 63 procedure Overwrite (Source : in out Bounded_String; Position : in Positive; New_Item : in String; Drop : in Truncation := Error); 64 function Delete (Source : in Bounded_String; From : in Positive; Through : in Natural) return Bounded_String; 65 procedure Delete (Source : in out Bounded_String; From : in Positive; Through : in Natural); 66 --String selector subprograms 67 function Trim (Source : in Bounded_String; Side : in Trim_End) return Bounded_String; procedure Trim (Source : in out Bounded_String; Side : in Trim_End); 68 function Trim (Source : in Bounded_String; Left : in Maps.Character_Set; Right : in Maps.Character_Set) return Bounded_String; 69 procedure Trim (Source : in out Bounded_String; Left : in Maps.Character_Set; Right : in Maps.Character_Set); 70 function Head (Source : in Bounded_String; Count : in Natural; Pad : in Character := Space; Drop : in Truncation := Error) return Bounded_String; 71 procedure Head (Source : in out Bounded_String; Count : in Natural; Pad : in Character := Space; Drop : in Truncation := Error); 72 function Tail (Source : in Bounded_String; Count : in Natural; Pad : in Character := Space; Drop : in Truncation := Error) return Bounded_String; 73 procedure Tail (Source : in out Bounded_String; Count : in Natural; Pad : in Character := Space; Drop : in Truncation := Error); 74 --String constructor subprograms 75 function "*" (Left : in Natural; Right : in Character) return Bounded_String; 76 function "*" (Left : in Natural; Right : in String) return Bounded_String; 77 function "*" (Left : in Natural; Right : in Bounded_String) return Bounded_String; 78 function Replicate (Count : in Natural; Item : in Character; Drop : in Truncation := Error) return Bounded_String; 79 function Replicate (Count : in Natural; Item : in String; Drop : in Truncation := Error) return Bounded_String; 80 function Replicate (Count : in Natural; Item : in Bounded_String; Drop : in Truncation := Error) return Bounded_String; 81 private ... -- not specified by the language end Generic_Bounded_Length; 82 end Ada.Strings.Bounded;

82.a.1/2
This paragraph was deleted.{8652/0097}
83

Null_Bounded_String represents the null string. If an object of type Bounded_String is not otherwise initialized, it will be initialized to the same value as Null_Bounded_String.

84

function Length (Source : in Bounded_String) return Length_Range;

85

The Length function returns the length of the string represented by Source.

86

function To_Bounded_String (Source : in String; Drop : in Truncation := Error) return Bounded_String;

87/3

If Source'Length <= Max_Length, then this function returns a Bounded_String that represents Source. Otherwise, the effect depends on the value of Drop:

88
  • If Drop=Left, then the result is a Bounded_String that represents the string comprising the rightmost Max_Length characters of Source.
  • 89
  • If Drop=Right, then the result is a Bounded_String that represents the string comprising the leftmost Max_Length characters of Source.
  • 90
  • If Drop=Error, then Strings.Length_Error is propagated.
91

function To_String (Source : in Bounded_String) return String;

92

To_String returns the String value with lower bound 1 represented by Source. If B is a Bounded_String, then B = To_Bounded_String(To_String(B)).

92.1/2

procedure Set_Bounded_String (Target : out Bounded_String; Source : in String; Drop : in Truncation := Error);

92.2/2

Equivalent to Target := To_Bounded_String (Source, Drop);

93

Each of the Append functions returns a Bounded_String obtained by concatenating the string or character given or represented by one of the parameters, with the string or character given or represented by the other parameter, and applying To_Bounded_String to the concatenation result string, with Drop as provided to the Append function.

94

Each of the procedures Append(Source, New_Item, Drop) has the same effect as the corresponding assignment Source := Append(Source, New_Item, Drop).

95

Each of the "&" functions has the same effect as the corresponding Append function, with Error as the Drop parameter.

96

function Element (Source : in Bounded_String; Index : in Positive) return Character;

97

Returns the character at position Index in the string represented by Source; propagates Index_Error if Index > Length(Source).

98

procedure Replace_Element (Source : in out Bounded_String; Index : in Positive; By : in Character);

99

Updates Source such that the character at position Index in the string represented by Source is By; propagates Index_Error if Index > Length(Source).

100

function Slice (Source : in Bounded_String; Low : in Positive; High : in Natural) return String;

101/1

{8652/0049} Returns the slice at positions Low through High in the string represented by Source; propagates Index_Error if Low > Length(Source)+1 or High > Length(Source). The bounds of the returned string are Low and High.

101.1/2

function Bounded_Slice (Source : in Bounded_String; Low : in Positive; High : in Natural) return Bounded_String;

101.2/2

Returns the slice at positions Low through High in the string represented by Source as a bounded string; propagates Index_Error if Low > Length(Source)+1 or High > Length(Source).

101.3/2

procedure Bounded_Slice (Source : in Bounded_String; Target : out Bounded_String; Low : in Positive; High : in Natural);

101.4/2

Equivalent to Target := Bounded_Slice (Source, Low, High);

102

Each of the functions "=", "<", ">", "<=", and ">=" returns the same result as the corresponding String operation applied to the String values given or represented by the two parameters.

103

Each of the search subprograms (Index, Index_Non_Blank, Count, Find_Token) has the same effect as the corresponding subprogram in Strings.Fixed applied to the string represented by the Bounded_String parameter.

104

Each of the Translate subprograms, when applied to a Bounded_String, has an analogous effect to the corresponding subprogram in Strings.Fixed. For the Translate function, the translation is applied to the string represented by the Bounded_String parameter, and the result is converted (via To_Bounded_String) to a Bounded_String. For the Translate procedure, the string represented by the Bounded_String parameter after the translation is given by the Translate function for fixed-length strings applied to the string represented by the original value of the parameter.

105/1

{8652/0049} Each of the transformation subprograms (Replace_Slice, Insert, Overwrite, Delete), selector subprograms (Trim, Head, Tail), and constructor functions ("*") has an effect based on its corresponding subprogram in Strings.Fixed, and Replicate is based on Fixed."*". In the case of a function, the corresponding fixed-length string subprogram is applied to the string represented by the Bounded_String parameter. To_Bounded_String is applied the result string, with Drop (or Error in the case of Generic_Bounded_Length."*") determining the effect when the string length exceeds Max_Length. In the case of a procedure, the corresponding function in Strings.Bounded.Generic_Bounded_Length is applied, with the result assigned into the Source parameter.

105.a/2
ramification

The "/=" operations between Bounded_String and String, and between String and Bounded_String, are automatically defined based on the corresponding "=" operations.

Implementation Advice

106

Bounded string objects should not be implemented by implicit pointers and dynamic allocation.

106.a.1/2
implementation advice

Bounded string objects should not be implemented by implicit pointers and dynamic allocation.

106.a
implementation note

The following is a possible implementation of the private part of the package:

106.b

type Bounded_String_Internals (Length : Length_Range := 0) is record Data : String(1..Length); end record; 106.c type Bounded_String is record Data : Bounded_String_Internals; -- Unconstrained end record; 106.d Null_Bounded_String : constant Bounded_String := (Data => (Length => 0, Data => (1..0 => ' ')));

Inconsistencies With Ada 95

106.e/2
correction

Amendment The bounds of the string returned from Slice are now defined. This is technically an inconsistency; if a program depended on some other lower bound for the string returned from Slice, it could fail when compiled with Ada 2005. Such code is not portable even between Ada 95 implementations, so it should be very rare.

Incompatibilities With Ada 95

106.f/3

Procedure Set_Bounded_String, two Bounded_Slice subprograms, and overloaded versions of Index and Index_Non_Blank are added to Strings.Bounded.Generic_Bounded_Length. If an instance of Generic_Bounded_Length is referenced in a use_clause, and an entity E with the defining_identifier as a new entity in Generic_Bounded_Length is defined in a package that is also referenced in a use_clause, the entity E may no longer be use-visible, resulting in errors. This should be rare and is easily fixed if it does occur.

Wording Changes from Ada 95

106.g/2

{8652/0049} Corrigendum: Corrected the conditions for which Slice raises Index_Error.

106.h/2

{8652/0049} Corrigendum: Clarified the meaning of transformation, selector, and constructor subprograms by describing the effects of procedures and functions separately.

Incompatibilities With Ada 2005

106.i/3

An overloaded version of Find_Token is added to Strings.Bounded.Generic_Bounded_Length. If an instance of Generic_Bounded_Length is referenced in a use_clause, and an entity E with a defining_identifier of Find_Token is defined in a package that is also referenced in a use_clause, the entity E may no longer be use-visible, resulting in errors. This should be rare and is easily fixed if it does occur.

A.4.5 Unbounded-Length String Handling

1/5

The language-defined package Strings.Unbounded provides a private type Unbounded_String and a set of operations. An object of type Unbounded_String represents a String whose low bound is 1 and whose length can vary conceptually between 0 and Natural'Last. The subprograms for fixed-length string handling are either overloaded directly for Unbounded_String, or are modified as necessary to reflect the flexibility in length. Since the Unbounded_String type is private, relevant constructor and selector operations are provided.

1.a
reason

The transformation operations for fixed- and bounded-length strings that are not necessarily length preserving are supplied for Unbounded_String as procedures as well as functions. This allows an implementation to do an initial allocation for an unbounded string and to avoid further allocations as long as the length does not exceed the allocated length.

Static Semantics

2

The library package Strings.Unbounded has the following declaration:

3/5

with Ada.Strings.Maps; package Ada.Strings.Unbounded with Preelaborate, Nonblocking, Global => in out synchronized is 4/5

type Unbounded_String is private with Preelaborable_Initialization; 5 Null_Unbounded_String : constant Unbounded_String; 6 function Length (Source : in Unbounded_String) return Natural; 7 type String_Access is access all String; procedure Free (X : in out String_Access); 8 -- Conversion, Concatenation, and Selection functions 9 function To_Unbounded_String (Source : in String) return Unbounded_String; 10 function To_Unbounded_String (Length : in Natural) return Unbounded_String; 11 function To_String (Source : in Unbounded_String) return String; 11.1/2

procedure Set_Unbounded_String (Target : out Unbounded_String; Source : in String); 12 procedure Append (Source : in out Unbounded_String; New_Item : in Unbounded_String); 13 procedure Append (Source : in out Unbounded_String; New_Item : in String); 14 procedure Append (Source : in out Unbounded_String; New_Item : in Character); 15 function "&" (Left, Right : in Unbounded_String) return Unbounded_String; 16 function "&" (Left : in Unbounded_String; Right : in String) return Unbounded_String; 17 function "&" (Left : in String; Right : in Unbounded_String) return Unbounded_String; 18 function "&" (Left : in Unbounded_String; Right : in Character) return Unbounded_String; 19 function "&" (Left : in Character; Right : in Unbounded_String) return Unbounded_String; 20 function Element (Source : in Unbounded_String; Index : in Positive) return Character; 21 procedure Replace_Element (Source : in out Unbounded_String; Index : in Positive; By : in Character); 22 function Slice (Source : in Unbounded_String; Low : in Positive; High : in Natural) return String; 22.1/2

function Unbounded_Slice (Source : in Unbounded_String; Low : in Positive; High : in Natural) return Unbounded_String; 22.2/2

procedure Unbounded_Slice (Source : in Unbounded_String; Target : out Unbounded_String; Low : in Positive; High : in Natural); 23 function "=" (Left, Right : in Unbounded_String) return Boolean; 24 function "=" (Left : in Unbounded_String; Right : in String) return Boolean; 25 function "=" (Left : in String; Right : in Unbounded_String) return Boolean; 26 function "<" (Left, Right : in Unbounded_String) return Boolean; 27 function "<" (Left : in Unbounded_String; Right : in String) return Boolean; 28 function "<" (Left : in String; Right : in Unbounded_String) return Boolean; 29 function "<=" (Left, Right : in Unbounded_String) return Boolean; 30 function "<=" (Left : in Unbounded_String; Right : in String) return Boolean; 31 function "<=" (Left : in String; Right : in Unbounded_String) return Boolean; 32 function ">" (Left, Right : in Unbounded_String) return Boolean; 33 function ">" (Left : in Unbounded_String; Right : in String) return Boolean; 34 function ">" (Left : in String; Right : in Unbounded_String) return Boolean; 35 function ">=" (Left, Right : in Unbounded_String) return Boolean; 36 function ">=" (Left : in Unbounded_String; Right : in String) return Boolean; 37 function ">=" (Left : in String; Right : in Unbounded_String) return Boolean; 38 -- Search subprograms 38.1/2

function Index (Source : in Unbounded_String; Pattern : in String; From : in Positive; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; 38.2/2

function Index (Source : in Unbounded_String; Pattern : in String; From : in Positive; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping_Function) return Natural; 39 function Index (Source : in Unbounded_String; Pattern : in String; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; 40 function Index (Source : in Unbounded_String; Pattern : in String; Going : in Direction := Forward; Mapping : in Maps.Character_Mapping_Function) return Natural; 40.1/2

function Index (Source : in Unbounded_String; Set : in Maps.Character_Set; From : in Positive; Test : in Membership := Inside; Going : in Direction := Forward) return Natural; 41 function Index (Source : in Unbounded_String; Set : in Maps.Character_Set; Test : in Membership := Inside; Going : in Direction := Forward) return Natural; 41.1/2

function Index_Non_Blank (Source : in Unbounded_String; From : in Positive; Going : in Direction := Forward) return Natural; 42 function Index_Non_Blank (Source : in Unbounded_String; Going : in Direction := Forward) return Natural; 43 function Count (Source : in Unbounded_String; Pattern : in String; Mapping : in Maps.Character_Mapping := Maps.Identity) return Natural; 44 function Count (Source : in Unbounded_String; Pattern : in String; Mapping : in Maps.Character_Mapping_Function) return Natural; 45 function Count (Source : in Unbounded_String; Set : in Maps.Character_Set) return Natural; 45.1/3

procedure Find_Token (Source : in Unbounded_String; Set : in Maps.Character_Set; From : in Positive; Test : in Membership; First : out Positive; Last : out Natural); 46 procedure Find_Token (Source : in Unbounded_String; Set : in Maps.Character_Set; Test : in Membership; First : out Positive; Last : out Natural); 47 -- String translation subprograms 48 function Translate (Source : in Unbounded_String; Mapping : in Maps.Character_Mapping) return Unbounded_String; 49 procedure Translate (Source : in out Unbounded_String; Mapping : in Maps.Character_Mapping); 50 function Translate (Source : in Unbounded_String; Mapping : in Maps.Character_Mapping_Function) return Unbounded_String; 51 procedure Translate (Source : in out Unbounded_String; Mapping : in Maps.Character_Mapping_Function); 52 -- String transformation subprograms 53 function Replace_Slice (Source : in Unbounded_String; Low : in Positive; High : in Natural; By : in String) return Unbounded_String; 54 procedure Replace_Slice (Source : in out Unbounded_String; Low : in Positive; High : in Natural; By : in String); 55 function Insert (Source : in Unbounded_String; Before : in Positive; New_Item : in String) return Unbounded_String; 56 procedure Insert (Source : in out Unbounded_String; Before : in Positive; New_Item : in String); 57 function Overwrite (Source : in Unbounded_String; Position : in Positive; New_Item : in String) return Unbounded_String; 58 procedure Overwrite (Source : in out Unbounded_String; Position : in Positive; New_Item : in String); 59 function Delete (Source : in Unbounded_String; From : in Positive; Through : in Natural) return Unbounded_String; 60 procedure Delete (Source : in out Unbounded_String; From : in Positive; Through : in Natural); 61 function Trim (Source : in Unbounded_String; Side : in Trim_End) return Unbounded_String; 62 procedure Trim (Source : in out Unbounded_String; Side : in Trim_End); 63 function Trim (Source : in Unbounded_String; Left : in Maps.Character_Set; Right : in Maps.Character_Set) return Unbounded_String; 64 procedure Trim (Source : in out Unbounded_String; Left : in Maps.Character_Set; Right : in Maps.Character_Set); 65 function Head (Source : in Unbounded_String; Count : in Natural; Pad : in Character := Space) return Unbounded_String; 66 procedure Head (Source : in out Unbounded_String; Count : in Natural; Pad : in Character := Space); 67 function Tail (Source : in Unbounded_String; Count : in Natural; Pad : in Character := Space) return Unbounded_String; 68 procedure Tail (Source : in out Unbounded_String; Count : in Natural; Pad : in Character := Space); 69 function "*" (Left : in Natural; Right : in Character) return Unbounded_String; 70 function "*" (Left : in Natural; Right : in String) return Unbounded_String; 71 function "*" (Left : in Natural; Right : in Unbounded_String) return Unbounded_String; 72 private ... -- not specified by the language end Ada.Strings.Unbounded;

72.1/2

The type Unbounded_String needs finalization (see 7.6).

73

Null_Unbounded_String represents the null String. If an object of type Unbounded_String is not otherwise initialized, it will be initialized to the same value as Null_Unbounded_String.

74

The function Length returns the length of the String represented by Source.

75

The type String_Access provides a (nonprivate) access type for explicit processing of unbounded-length strings. The procedure Free performs an unchecked deallocation of an object of type String_Access.

76

The function To_Unbounded_String(Source : in String) returns an Unbounded_String that represents Source. The function To_Unbounded_String(Length : in Natural) returns an Unbounded_String that represents an uninitialized String whose length is Length.

77

The function To_String returns the String with lower bound 1 represented by Source. To_String and To_Unbounded_String are related as follows:

78
  • If S is a String, then To_String(To_Unbounded_String(S)) = S.
  • 79
  • If U is an Unbounded_String, then To_Unbounded_String(To_String(U)) = U.
79.1/2

The procedure Set_Unbounded_String sets Target to an Unbounded_String that represents Source.

80

For each of the Append procedures, the resulting string represented by the Source parameter is given by the concatenation of the original value of Source and the value of New_Item.

81

Each of the "&" functions returns an Unbounded_String obtained by concatenating the string or character given or represented by one of the parameters, with the string or character given or represented by the other parameter, and applying To_Unbounded_String to the concatenation result string.

81.a/5
ramification

If the resulting string is longer than Natural'Last, Constraint_Error is raised because the upper bound of the underlying String concatenation is outside of the range of the index subtype of Natural (see 4.5.3). Note that the same is true for other operations that attempt to create an overlong string: either they are defined in terms of this concatenation operation (as with Append) or they are defined in terms of Ada.Strings.Fixed operations (as with Insert and Replace_Slice), which themselves are defined in terms of String concatenation (which raises Constraint_Error for overlong strings as described above). Therefore, it is never possible to create an Unbounded_String with a length greater than Natural'Last.

82

The Element, Replace_Element, and Slice subprograms have the same effect as the corresponding bounded-length string subprograms.

82.1/3

The function Unbounded_Slice returns the slice at positions Low through High in the string represented by Source as an Unbounded_String. The procedure Unbounded_Slice sets Target to the Unbounded_String representing the slice at positions Low through High in the string represented by Source. Both subprograms propagate Index_Error if Low > Length(Source)+1 or High > Length(Source).

83

Each of the functions "=", "<", ">", "<=", and ">=" returns the same result as the corresponding String operation applied to the String values given or represented by Left and Right.

84

Each of the search subprograms (Index, Index_Non_Blank, Count, Find_Token) has the same effect as the corresponding subprogram in Strings.Fixed applied to the string represented by the Unbounded_String parameter.

85

The Translate function has an analogous effect to the corresponding subprogram in Strings.Fixed. The translation is applied to the string represented by the Unbounded_String parameter, and the result is converted (via To_Unbounded_String) to an Unbounded_String.

86

Each of the transformation functions (Replace_Slice, Insert, Overwrite, Delete), selector functions (Trim, Head, Tail), and constructor functions ("*") is likewise analogous to its corresponding subprogram in Strings.Fixed. For each of the subprograms, the corresponding fixed-length string subprogram is applied to the string represented by the Unbounded_String parameter, and To_Unbounded_String is applied the result string.

87

For each of the procedures Translate, Replace_Slice, Insert, Overwrite, Delete, Trim, Head, and Tail, the resulting string represented by the Source parameter is given by the corresponding function for fixed-length strings applied to the string represented by Source's original value.

Implementation Requirements

88

No storage associated with an Unbounded_String object shall be lost upon assignment or scope exit.

88.a/2
implementation note

A sample implementation of the private part of the package and several of the subprograms appears in the Ada 95 Rationale.

Incompatibilities With Ada 95

88.b/2
correction

Amendment Type Unbounded_String is defined to need finalization. If the restriction No_Nested_Finalization (see D.7) applies to the partition, and Unbounded_String does not have a controlled part, it will not be allowed in local objects in Ada 2005 whereas it would be allowed in original Ada 95. Such code is not portable, as most Ada compilers have a controlled part in Unbounded_String, and thus would be illegal.

88.c/3

Procedure Set_Unbounded_String, two Unbounded_Slice subprograms, and overloaded versions of Index and Index_Non_Blank are added to Strings.Unbounded. If Strings.Unbounded is referenced in a use_clause, and an entity E with the same defining_identifier as a new entity in Strings.Unbounded is defined in a package that is also referenced in a use_clause, the entity E may no longer be use-visible, resulting in errors. This should be rare and is easily fixed if it does occur.

Extensions to Ada 95

88.d/2
correction

Amendment Added a pragma Preelaborable_Initialization to type Unbounded_String, so that it can be used to declare default-initialized objects in preelaborated units.

Incompatibilities With Ada 2005

88.e/3

An overloaded version of Find_Token is added to Strings.Unbounded. If Strings.Unbounded is referenced in a use_clause, and an entity E with a defining_identifier of Find_Token is defined in a package that is also referenced in a use_clause, the entity E may no longer be use-visible, resulting in errors. This should be rare and is easily fixed if it does occur.

A.4.6 String-Handling Sets and Mappings

1

The language-defined package Strings.Maps.Constants declares Character_Set and Character_Mapping constants corresponding to classification and conversion functions in package Characters.Handling.

1.a
discussion

The Constants package is a child of Strings.Maps since it needs visibility of the private part of Strings.Maps in order to initialize the constants in a preelaborable way (i.e. via aggregates versus function calls).

Static Semantics

2

The library package Strings.Maps.Constants has the following declaration:

3/5

package Ada.Strings.Maps.Constants with Pure is 4 Control_Set : constant Character_Set; Graphic_Set : constant Character_Set; Letter_Set : constant Character_Set; Lower_Set : constant Character_Set; Upper_Set : constant Character_Set; Basic_Set : constant Character_Set; Decimal_Digit_Set : constant Character_Set; Hexadecimal_Digit_Set : constant Character_Set; Alphanumeric_Set : constant Character_Set; Special_Set : constant Character_Set; ISO_646_Set : constant Character_Set; 5 Lower_Case_Map : constant Character_Mapping; --Maps to lower case for letters, else identity Upper_Case_Map : constant Character_Mapping; --Maps to upper case for letters, else identity Basic_Map : constant Character_Mapping; --Maps to basic letter for letters, else identity 6 private ... -- not specified by the language end Ada.Strings.Maps.Constants;

7

Each of these constants represents a correspondingly named set of characters or character mapping in Characters.Handling (see A.3.2).

8/5

NOTE There are certain characters which are defined to be lower case letters by ISO/IEC 10646 and are therefore allowed in identifiers, but are not considered lower case letters by Ada.Strings.Maps.Constants.

8.a/3
reason

This is to maintain runtime compatibility with the Ada 95 definitions of these constants; existing correct programs could break if the definitions were changed in a way the programs did not anticipate.

Extensions to Ada 95

8.b/2

Strings.Maps.Constants is now Pure, so it can be used in pure units.

Wording Changes from Ada 2005

8.c/3
correction

Added a note to clarify that these constants don't have any relationship to the characters allowed in identifiers.

A.4.7 Wide_String Handling

1/3

Facilities for handling strings of Wide_Character elements are found in the packages Strings.Wide_Maps, Strings.Wide_Fixed, Strings.Wide_Bounded, Strings.Wide_Unbounded, and Strings.Wide_Maps.Wide_Constants, and in the library functions Strings.Wide_Hash, Strings.Wide_Fixed.Wide_Hash, Strings.Wide_Bounded.Wide_Hash, Strings.Wide_Unbounded.Wide_Hash, Strings.Wide_Hash_Case_Insensitive, Strings.Wide_Fixed.Wide_Hash_Case_Insensitive, Strings.Wide_Bounded.Wide_Hash_Case_Insensitive, Strings.Wide_Unbounded.Wide_Hash_Case_Insensitive, Strings.Wide_Equal_Case_Insensitive, Strings.Wide_Fixed.Wide_Equal_Case_Insensitive, Strings.Wide_Bounded.Wide_Equal_Case_Insensitive, and Strings.Wide_Unbounded.Wide_Equal_Case_Insensitive. They provide the same string-handling operations as the corresponding packages and functions for strings of Character elements.

Static Semantics

2

The package Strings.Wide_Maps has the following declaration.

3/5

package Ada.Strings.Wide_Maps with Preelaborate, Nonblocking, Global => in out synchronized is 4/5

-- Representation for a set of Wide_Character values: type Wide_Character_Set is private with Preelaborable_Initialization; 5 Null_Set : constant Wide_Character_Set; 6 type Wide_Character_Range is record Low : Wide_Character; High : Wide_Character; end record; -- Represents Wide_Character range Low..High 7 type Wide_Character_Ranges is array (Positive range <>) of Wide_Character_Range; 8 function To_Set (Ranges : in Wide_Character_Ranges) return Wide_Character_Set; 9 function To_Set (Span : in Wide_Character_Range) return Wide_Character_Set; 10 function To_Ranges (Set : in Wide_Character_Set) return Wide_Character_Ranges; 11 function "=" (Left, Right : in Wide_Character_Set) return Boolean; 12 function "not" (Right : in Wide_Character_Set) return Wide_Character_Set; function "and" (Left, Right : in Wide_Character_Set) return Wide_Character_Set; function "or" (Left, Right : in Wide_Character_Set) return Wide_Character_Set; function "xor" (Left, Right : in Wide_Character_Set) return Wide_Character_Set; function "-" (Left, Right : in Wide_Character_Set) return Wide_Character_Set; 13 function Is_In (Element : in Wide_Character; Set : in Wide_Character_Set) return Boolean; 14 function Is_Subset (Elements : in Wide_Character_Set; Set : in Wide_Character_Set) return Boolean; 15 function "<=" (Left : in Wide_Character_Set; Right : in Wide_Character_Set) return Boolean renames Is_Subset; 16 -- Alternative representation for a set of Wide_Character values: subtype Wide_Character_Sequence is Wide_String; 17 function To_Set (Sequence : in Wide_Character_Sequence) return Wide_Character_Set; 18 function To_Set (Singleton : in Wide_Character) return Wide_Character_Set; 19 function To_Sequence (Set : in Wide_Character_Set) return Wide_Character_Sequence; 20/5

-- Representation for a Wide_Character to Wide_Character mapping: type Wide_Character_Mapping is private with Preelaborable_Initialization; 21 function Value (Map : in Wide_Character_Mapping; Element : in Wide_Character) return Wide_Character; 22 Identity : constant Wide_Character_Mapping; 23 function To_Mapping (From, To : in Wide_Character_Sequence) return Wide_Character_Mapping; 24 function To_Domain (Map : in Wide_Character_Mapping) return Wide_Character_Sequence; 25 function To_Range (Map : in Wide_Character_Mapping) return Wide_Character_Sequence; 26 type Wide_Character_Mapping_Function is access function (From : in Wide_Character) return Wide_Character; 27 private ... -- not specified by the language end Ada.Strings.Wide_Maps;

28

The context clause for each of the packages Strings.Wide_Fixed, Strings.Wide_Bounded, and Strings.Wide_Unbounded identifies Strings.Wide_Maps instead of Strings.Maps.

28.1/3

Types Wide_Character_Set and Wide_Character_Mapping need finalization.

29/3

For each of the packages Strings.Fixed, Strings.Bounded, Strings.Unbounded, and Strings.Maps.Constants, and for library functions Strings.Hash, Strings.Fixed.Hash, Strings.Bounded.Hash, Strings.Unbounded.Hash, Strings.Hash_Case_Insensitive, Strings.Fixed.Hash_Case_Insensitive, Strings.Bounded.Hash_Case_Insensitive, Strings.Unbounded.Hash_Case_Insensitive, Strings.Equal_Case_Insensitive, Strings.Fixed.Equal_Case_Insensitive, Strings.Bounded.Equal_Case_Insensitive, and Strings.Unbounded.Equal_Case_Insensitive, the corresponding wide string package or function has the same contents except that

30
  • Wide_Space replaces Space
  • 31
  • Wide_Character replaces Character
  • 32
  • Wide_String replaces String
  • 33
  • Wide_Character_Set replaces Character_Set
  • 34
  • Wide_Character_Mapping replaces Character_Mapping
  • 35
  • Wide_Character_Mapping_Function replaces Character_Mapping_Function
  • 36
  • Wide_Maps replaces Maps
  • 37
  • Bounded_Wide_String replaces Bounded_String
  • 38
  • Null_Bounded_Wide_String replaces Null_Bounded_String
  • 39
  • To_Bounded_Wide_String replaces To_Bounded_String
  • 40
  • To_Wide_String replaces To_String
  • 40.1/2
  • Set_Bounded_Wide_String replaces Set_Bounded_String
  • 41
  • Unbounded_Wide_String replaces Unbounded_String
  • 42
  • Null_Unbounded_Wide_String replaces Null_Unbounded_String
  • 43
  • Wide_String_Access replaces String_Access
  • 44
  • To_Unbounded_Wide_String replaces To_Unbounded_String
  • 44.1/2
  • Set_Unbounded_Wide_String replaces Set_Unbounded_String
45

The following additional declaration is present in Strings.Wide_Maps.Wide_Constants:

46/2

Character_Set : constant Wide_Maps.Wide_Character_Set; --Contains each Wide_Character value WC such that --Characters.Conversions.Is_Character(WC) is True

46.1/2

Each Wide_Character_Set constant in the package Strings.Wide_Maps.Wide_Constants contains no values outside the Character portion of Wide_Character. Similarly, each Wide_Character_Mapping constant in this package is the identity mapping when applied to any element outside the Character portion of Wide_Character.

47/5

Aspect Pure is replaced by aspects Preelaborate, Nonblocking, Global => in out synchronized in Strings.Wide_Maps.Wide_Constants.

48/5

NOTE If a null Wide_Character_Mapping_Function is passed to any of the Wide_String handling subprograms, Constraint_Error is propagated.

Incompatibilities With Ada 95

49.a/2

Various new operations are added to Strings.Wide_Fixed, Strings.Wide_Bounded, and Strings.Wide_Unbounded. If one of these packages is referenced in a use_clause, and an entity E with the same defining_identifier as a new entity is defined in a package that is also referenced in a use_clause, the entity E may no longer be use-visible, resulting in errors. This should be rare and is easily fixed if it does occur.

Extensions to Ada 95

49.b/2
correction

Amendment Added pragma Preelaborable_Initialization to types Wide_Character_Set and Wide_Character_Mapping, so that they can be used to declare default-initialized objects in preelaborated units.

Wording Changes from Ada 95

49.c/2

Corrected the description of Character_Set.

49.d/2

Added wide versions of Strings.Hash and Strings.Unbounded.Hash.

49.e/2

Added wording so that Strings.Wide_Maps.Wide_Constants does not change to Pure.

49.f/2

The second Note is now normative text, since there is no way to derive it from the other rules. It's a little weird given the use of Unicode character classifications in Ada 2005; but changing it would be inconsistent with Ada 95 and a one-to-one mapping isn't necessarily correct anyway.

Extensions to Ada 2005

49.g/3

The case insenstive library functions (Strings.Wide_Equal_Case_Insensitive, Strings.Wide_Fixed.Wide_Equal_Case_Insensitive, Strings.Wide_Bounded.Wide_Equal_Case_Insensitive, Strings.Wide_Unbounded.Wide_Equal_Case_Insensitive, Strings.Wide_Hash_Case_Insensitive, Strings.Wide_Fixed.Wide_Hash_Case_Insensitive, Strings.Wide_Bounded.Wide_Hash_Case_Insensitive, and Strings.Wide_Unbounded.Wide_Hash_Case_Insensitive) are new.

Wording Changes from Ada 2005

49.h/3

Correction: Identified Wide_Character_Set and Wide_Character_Mapping as needing finalization. It is likely that they are implemented with a controlled type, so this change is unlikely to make any difference in practice.

A.4.8 Wide_Wide_String Handling

1/3

Facilities for handling strings of Wide_Wide_Character elements are found in the packages Strings.Wide_Wide_Maps, Strings.Wide_Wide_Fixed, Strings.Wide_Wide_Bounded, Strings.Wide_Wide_Unbounded, and Strings.Wide_Wide_Maps.Wide_Wide_Constants, and in the library functions Strings.Wide_Wide_Hash, Strings.Wide_Wide_Fixed.Wide_Wide_Hash, Strings.Wide_Wide_Bounded.Wide_Wide_Hash, Strings.Wide_Wide_Unbounded.Wide_Wide_Hash, Strings.Wide_Wide_Hash_Case_Insensitive, Strings.Wide_Wide_Fixed.Wide_Wide_Hash_Case_Insensitive, Strings.Wide_Wide_Bounded.Wide_Wide_Hash_Case_Insensitive, Strings.Wide_Wide_Unbounded.Wide_Wide_Hash_Case_Insensitive, Strings.Wide_Wide_Equal_Case_Insensitive, Strings.Wide_Wide_Fixed.Wide_Wide_Equal_Case_Insensitive, Strings.Wide_Wide_Bounded.Wide_Wide_Equal_Case_Insensitive, and Strings.Wide_Wide_Unbounded.Wide_Wide_Equal_Case_Insensitive. They provide the same string-handling operations as the corresponding packages and functions for strings of Character elements.

Static Semantics

2/2

The library package Strings.Wide_Wide_Maps has the following declaration.

3/5

package Ada.Strings.Wide_Wide_Maps with Preelaborate, Nonblocking, Global => in out synchronized is 4/5

-- Representation for a set of Wide_Wide_Character values: type Wide_Wide_Character_Set is private with Preelaborable_Initialization; 5/2 Null_Set : constant Wide_Wide_Character_Set; 6/2 type Wide_Wide_Character_Range is record Low : Wide_Wide_Character; High : Wide_Wide_Character; end record; -- Represents Wide_Wide_Character range Low..High 7/2 type Wide_Wide_Character_Ranges is array (Positive range <>) of Wide_Wide_Character_Range; 8/2 function To_Set (Ranges : in Wide_Wide_Character_Ranges) return Wide_Wide_Character_Set; 9/2 function To_Set (Span : in Wide_Wide_Character_Range) return Wide_Wide_Character_Set; 10/2 function To_Ranges (Set : in Wide_Wide_Character_Set) return Wide_Wide_Character_Ranges; 11/2 function "=" (Left, Right : in Wide_Wide_Character_Set) return Boolean; 12/2 function "not" (Right : in Wide_Wide_Character_Set) return Wide_Wide_Character_Set; function "and" (Left, Right : in Wide_Wide_Character_Set) return Wide_Wide_Character_Set; function "or" (Left, Right : in Wide_Wide_Character_Set) return Wide_Wide_Character_Set; function "xor" (Left, Right : in Wide_Wide_Character_Set) return Wide_Wide_Character_Set; function "-" (Left, Right : in Wide_Wide_Character_Set) return Wide_Wide_Character_Set; 13/2 function Is_In (Element : in Wide_Wide_Character; Set : in Wide_Wide_Character_Set) return Boolean; 14/2 function Is_Subset (Elements : in Wide_Wide_Character_Set; Set : in Wide_Wide_Character_Set) return Boolean; 15/2 function "<=" (Left : in Wide_Wide_Character_Set; Right : in Wide_Wide_Character_Set) return Boolean renames Is_Subset; 16/2 -- Alternative representation for a set of Wide_Wide_Character values: subtype Wide_Wide_Character_Sequence is Wide_Wide_String; 17/2 function To_Set (Sequence : in Wide_Wide_Character_Sequence) return Wide_Wide_Character_Set; 18/2 function To_Set (Singleton : in Wide_Wide_Character) return Wide_Wide_Character_Set; 19/2 function To_Sequence (Set : in Wide_Wide_Character_Set) return Wide_Wide_Character_Sequence; 20/5

-- Representation for a Wide_Wide_Character to Wide_Wide_Character -- mapping: type Wide_Wide_Character_Mapping is private with Preelaborable_Initialization; 21/2 function Value (Map : in Wide_Wide_Character_Mapping; Element : in Wide_Wide_Character) return Wide_Wide_Character; 22/2 Identity : constant Wide_Wide_Character_Mapping; 23/2 function To_Mapping (From, To : in Wide_Wide_Character_Sequence) return Wide_Wide_Character_Mapping; 24/2 function To_Domain (Map : in Wide_Wide_Character_Mapping) return Wide_Wide_Character_Sequence; 25/2 function To_Range (Map : in Wide_Wide_Character_Mapping) return Wide_Wide_Character_Sequence; 26/2 type Wide_Wide_Character_Mapping_Function is access function (From : in Wide_Wide_Character) return Wide_Wide_Character; 27/2 private ... -- not specified by the language end Ada.Strings.Wide_Wide_Maps;

28/2

The context clause for each of the packages Strings.Wide_Wide_Fixed, Strings.Wide_Wide_Bounded, and Strings.Wide_Wide_Unbounded identifies Strings.Wide_Wide_Maps instead of Strings.Maps.

28.1/3

Types Wide_Wide_Character_Set and Wide_Wide_Character_Mapping need finalization.

29/3

For each of the packages Strings.Fixed, Strings.Bounded, Strings.Unbounded, and Strings.Maps.Constants, and for library functions Strings.Hash, Strings.Fixed.Hash, Strings.Bounded.Hash, Strings.Unbounded.Hash, Strings.Hash_Case_Insensitive, Strings.Fixed.Hash_Case_Insensitive, Strings.Bounded.Hash_Case_Insensitive, Strings.Unbounded.Hash_Case_Insensitive, Strings.Equal_Case_Insensitive, Strings.Fixed.Equal_Case_Insensitive, Strings.Bounded.Equal_Case_Insensitive, and Strings.Unbounded.Equal_Case_Insensitive, the corresponding wide wide string package or function has the same contents except that

30/2
  • Wide_Wide_Space replaces Space
  • 31/2
  • Wide_Wide_Character replaces Character
  • 32/2
  • Wide_Wide_String replaces String
  • 33/2
  • Wide_Wide_Character_Set replaces Character_Set
  • 34/2
  • Wide_Wide_Character_Mapping replaces Character_Mapping
  • 35/2
  • Wide_Wide_Character_Mapping_Function replaces Character_Mapping_Function
  • 36/2
  • Wide_Wide_Maps replaces Maps
  • 37/2
  • Bounded_Wide_Wide_String replaces Bounded_String
  • 38/2
  • Null_Bounded_Wide_Wide_String replaces Null_Bounded_String
  • 39/2
  • To_Bounded_Wide_Wide_String replaces To_Bounded_String
  • 40/2
  • To_Wide_Wide_String replaces To_String
  • 41/2
  • Set_Bounded_Wide_Wide_String replaces Set_Bounded_String
  • 42/2
  • Unbounded_Wide_Wide_String replaces Unbounded_String
  • 43/2
  • Null_Unbounded_Wide_Wide_String replaces Null_Unbounded_String
  • 44/2
  • Wide_Wide_String_Access replaces String_Access
  • 45/2
  • To_Unbounded_Wide_Wide_String replaces To_Unbounded_String
  • 46/2
  • Set_Unbounded_Wide_Wide_String replaces Set_Unbounded_String
47/2

The following additional declarations are present in Strings.Wide_Wide_Maps.Wide_Wide_Constants:

48/2

Character_Set : constant Wide_Wide_Maps.Wide_Wide_Character_Set; -- Contains each Wide_Wide_Character value WWC such that -- Characters.Conversions.Is_Character(WWC) is True Wide_Character_Set : constant Wide_Wide_Maps.Wide_Wide_Character_Set; -- Contains each Wide_Wide_Character value WWC such that -- Characters.Conversions.Is_Wide_Character(WWC) is True

49/2

Each Wide_Wide_Character_Set constant in the package Strings.Wide_Wide_Maps.Wide_Wide_Constants contains no values outside the Character portion of Wide_Wide_Character. Similarly, each Wide_Wide_Character_Mapping constant in this package is the identity mapping when applied to any element outside the Character portion of Wide_Wide_Character.

50/5

Aspect Pure is replaced by aspects Preelaborate, Nonblocking, Global => in out synchronized in Strings.Wide_Wide_Maps.Wide_Wide_Constants.

51/2

NOTE If a null Wide_Wide_Character_Mapping_Function is passed to any of the Wide_Wide_String handling subprograms, Constraint_Error is propagated.

Extensions to Ada 95

51.a/2

The double-wide string-handling packages (Strings.Wide_Wide_Maps, Strings.Wide_Wide_Fixed, Strings.Wide_Wide_Bounded, Strings.Wide_Wide_Unbounded, and Strings.Wide_Wide_Maps.Wide_Wide_Constants), and functions Strings.Wide_Wide_Hash and Strings.Wide_Wide_Unbounded.Wide_Wide_Hash are new.

Extensions to Ada 2005

51.b/3

The case insenstive library functions (Strings.Wide_Wide_Equal_Case_Insensitive, Strings.Wide_Wide_Fixed.Wide_Wide_Equal_Case_Insensitive, Strings.Wide_Wide_Bounded.Wide_Wide_Equal_Case_Insensitive, Strings.Wide_Wide_Unbounded.Wide_Wide_Equal_Case_Insensitive, Strings.Wide_Wide_Hash_Case_Insensitive, Strings.Wide_Wide_Fixed.Wide_Wide_Hash_Case_Insensitive, Strings.Wide_Wide_Bounded.Wide_Wide_Hash_Case_Insensitive, and Strings.Wide_Wide_Unbounded.Wide_Wide_Hash_Case_Insensitive) are new.

Wording Changes from Ada 2005

51.c/3

Correction: Identified Wide_Wide_Character_Set and Wide_Wide_Character_Mapping as needing finalization. It is likely that they are implemented with a controlled type, so this change is unlikely to make any difference in practice.

A.4.9 String Hashing

Static Semantics

1/2

The library function Strings.Hash has the following declaration:

2/5

with Ada.Containers; function Ada.Strings.Hash (Key : String) return Containers.Hash_Type with Pure;

3/2

Returns an implementation-defined value which is a function of the value of Key. If A and B are strings such that A equals B, Hash(A) equals Hash(B).

3.a/2
implementation defined

The values returned by Strings.Hash.

4/2

The library function Strings.Fixed.Hash has the following declaration:

5/3

with Ada.Containers, Ada.Strings.Hash; function Ada.Strings.Fixed.Hash (Key : String) return Containers.Hash_Type renames Ada.Strings.Hash;

6/2

The generic library function Strings.Bounded.Hash has the following declaration:

7/5

with Ada.Containers; generic with package Bounded is new Ada.Strings.Bounded.Generic_Bounded_Length (<>); function Ada.Strings.Bounded.Hash (Key : Bounded.Bounded_String) return Containers.Hash_Type with Preelaborate, Nonblocking, Global => in out synchronized;

8/3

Equivalent to Strings.Hash (Bounded.To_String (Key));

9/2

The library function Strings.Unbounded.Hash has the following declaration:

10/5

with Ada.Containers; function Ada.Strings.Unbounded.Hash (Key : Unbounded_String) return Containers.Hash_Type with Preelaborate, Nonblocking, Global => in out synchronized;

11/3

Equivalent to Strings.Hash (To_String (Key));

11.1/3

The library function Strings.Hash_Case_Insensitive has the following declaration:

11.2/5

with Ada.Containers; function Ada.Strings.Hash_Case_Insensitive (Key : String) return Containers.Hash_Type with Pure;

11.3/3

Returns an implementation-defined value which is a function of the value of Key, converted to lower case. If A and B are strings such that Strings.Equal_Case_Insensitive (A, B) (see A.4.10) is True, then Hash_Case_Insensitive(A) equals Hash_Case_Insensitive(B).

11.4/3

The library function Strings.Fixed.Hash_Case_Insensitive has the following declaration:

11.5/3

with Ada.Containers, Ada.Strings.Hash_Case_Insensitive; function Ada.Strings.Fixed.Hash_Case_Insensitive (Key : String) return Containers.Hash_Type renames Ada.Strings.Hash_Case_Insensitive;

11.6/3

The generic library function Strings.Bounded.Hash_Case_Insensitive has the following declaration:

11.7/5

with Ada.Containers; generic with package Bounded is new Ada.Strings.Bounded.Generic_Bounded_Length (<>); function Ada.Strings.Bounded.Hash_Case_Insensitive (Key : Bounded.Bounded_String) return Containers.Hash_Type with Preelaborate, Nonblocking, Global => in out synchronized;

11.8/3

Equivalent to Strings.Hash_Case_Insensitive (Bounded.To_String (Key));

11.9/3

The library function Strings.Unbounded.Hash_Case_Insensitive has the following declaration:

11.10/5

with Ada.Containers; function Ada.Strings.Unbounded.Hash_Case_Insensitive (Key : Unbounded_String) return Containers.Hash_Type with Preelaborate, Nonblocking, Global => in out synchronized;

11.11/3

Equivalent to Strings.Hash_Case_Insensitive (To_String (Key));

Implementation Advice

12/2

The Hash functions should be good hash functions, returning a wide spread of values for different string values. It should be unlikely for similar strings to return the same value.

12.a/2
implementation advice

Strings.Hash should be good a hash function, returning a wide spread of values for different string values, and similar strings should rarely return the same value.

12.b/2
ramification

The other functions are defined in terms of Strings.Hash, so they don't need separate advice in the Annex.

Extensions to Ada 95

12.c/2

The Strings.Hash, Strings.Fixed.Hash, Strings.Bounded.Hash, and Strings.Unbounded.Hash functions are new.

Extensions to Ada 2005

12.d/3

The Strings.Hash_Case_Insensitive, Strings.Fixed.Hash_Case_Insensitive, Strings.Bounded.Hash_Case_Insensitive, and Strings.Unbounded.Hash_Case_Insensitive functions are new.

A.4.10 String Comparison

Static Semantics

1/3

The library function Strings.Equal_Case_Insensitive has the following declaration:

2/5

function Ada.Strings.Equal_Case_Insensitive (Left, Right : String) return Boolean with Pure;

3/5

Returns True if the strings consist of the same sequence of characters after applying locale-independent simple case folding, as defined by documents referenced in Clause 2 of ISO/IEC 10646:2020. Otherwise, returns False. This function uses the same method as is used to determine whether two identifiers are the same.

3.a/3
discussion

For String, this is equivalent to converting to lower case and comparing. Not so for other string types. For Wide_Strings and Wide_Wide_Strings, note that this result is a more accurate comparison than converting the strings to lower case and comparing the results; it is possible that the lower case conversions are the same but this routine will report the strings as different. Additionally, Unicode says that the result of this function will never change for strings made up solely of defined code points; there is no such guarantee for case conversion to lower case.

3.b/5

The “documents referenced” means Unicode, Chapter 4 (specifically, section 4.2 — Case). See the Implementation Notes in 2.3 for a source for machine-readable definitions of these properties.

4/3

The library function Strings.Fixed.Equal_Case_Insensitive has the following declaration:

5/3

with Ada.Strings.Equal_Case_Insensitive; function Ada.Strings.Fixed.Equal_Case_Insensitive (Left, Right : String) return Boolean renames Ada.Strings.Equal_Case_Insensitive;

6/3

The generic library function Strings.Bounded.Equal_Case_Insensitive has the following declaration:

7/5

generic with package Bounded is new Ada.Strings.Bounded.Generic_Bounded_Length (<>); function Ada.Strings.Bounded.Equal_Case_Insensitive (Left, Right : Bounded.Bounded_String) return Boolean with Preelaborate, Nonblocking, Global => in out synchronized;

8/3

Equivalent to Strings.Equal_Case_Insensitive (Bounded.To_String (Left), Bounded.To_String (Right));

9/3

The library function Strings.Unbounded.Equal_Case_Insensitive has the following declaration:

10/5

function Ada.Strings.Unbounded.Equal_Case_Insensitive (Left, Right : Unbounded_String) return Boolean with Preelaborate, Nonblocking, Global => in out synchronized;

11/3

Equivalent to Strings.Equal_Case_Insensitive (To_String (Left), To_String (Right));

12/3

The library function Strings.Less_Case_Insensitive has the following declaration:

13/5

function Ada.Strings.Less_Case_Insensitive (Left, Right : String) return Boolean with Pure;

14/3

Performs a lexicographic comparison of strings Left and Right, converted to lower case.

15/3

The library function Strings.Fixed.Less_Case_Insensitive has the following declaration:

16/3

with Ada.Strings.Less_Case_Insensitive; function Ada.Strings.Fixed.Less_Case_Insensitive (Left, Right : String) return Boolean renames Ada.Strings.Less_Case_Insensitive;

17/3

The generic library function Strings.Bounded.Less_Case_Insensitive has the following declaration:

18/5

generic with package Bounded is new Ada.Strings.Bounded.Generic_Bounded_Length (<>); function Ada.Strings.Bounded.Less_Case_Insensitive (Left, Right : Bounded.Bounded_String) return Boolean with Preelaborate, Nonblocking, Global => in out synchronized;

19/3

Equivalent to Strings.Less_Case_Insensitive (Bounded.To_String (Left), Bounded.To_String (Right));

20/3

The library function Strings.Unbounded.Less_Case_Insensitive has the following declaration:

21/5

function Ada.Strings.Unbounded.Less_Case_Insensitive (Left, Right : Unbounded_String) return Boolean with Preelaborate, Nonblocking, Global => in out synchronized;

22/3

Equivalent to Strings.Less_Case_Insensitive (To_String (Left), To_String (Right));

Extensions to Ada 2005

22.a/3

The Strings.Equal_Case_Insensitive, Strings.Fixed.Equal_Case_Insensitive, Strings.Bounded.Equal_Case_Insensitive, Strings.Unbounded.Equal_Case_Insensitive, Strings.Less_Case_Insensitive, Strings.Fixed.Less_Case_Insensitive, Strings.Bounded.Less_Case_Insensitive, Strings.Unbounded.Less_Case_Insensitive functions are new.

A.4.11 String Encoding

1/3

Facilities for encoding, decoding, and converting strings in various character encoding schemes are provided by packages Strings.UTF_Encoding, Strings.UTF_Encoding.Conversions, Strings.UTF_Encoding.Strings, Strings.UTF_Encoding.Wide_Strings, and Strings.UTF_Encoding.Wide_Wide_Strings.

Static Semantics

2/3

The encoding library packages have the following declarations:

3/5

package Ada.Strings.UTF_Encoding with Pure is 4/3 -- Declarations common to the string encoding packages type Encoding_Scheme is (UTF_8, UTF_16BE, UTF_16LE); 5/3 subtype UTF_String is String; 6/3 subtype UTF_8_String is String; 7/3 subtype UTF_16_Wide_String is Wide_String; 8/3 Encoding_Error : exception; 9/3 BOM_8 : constant UTF_8_String := Character'Val(16#EF#) & Character'Val(16#BB#) & Character'Val(16#BF#); 10/3 BOM_16BE : constant UTF_String := Character'Val(16#FE#) & Character'Val(16#FF#); 11/3 BOM_16LE : constant UTF_String := Character'Val(16#FF#) & Character'Val(16#FE#); 12/3 BOM_16 : constant UTF_16_Wide_String := (1 => Wide_Character'Val(16#FEFF#)); 13/3 function Encoding (Item : UTF_String; Default : Encoding_Scheme := UTF_8) return Encoding_Scheme; 14/3 end Ada.Strings.UTF_Encoding; 15/5

package Ada.Strings.UTF_Encoding.Conversions with Pure is 16/3 -- Conversions between various encoding schemes function Convert (Item : UTF_String; Input_Scheme : Encoding_Scheme; Output_Scheme : Encoding_Scheme; Output_BOM : Boolean := False) return UTF_String; 17/3 function Convert (Item : UTF_String; Input_Scheme : Encoding_Scheme; Output_BOM : Boolean := False) return UTF_16_Wide_String; 18/3 function Convert (Item : UTF_8_String; Output_BOM : Boolean := False) return UTF_16_Wide_String; 19/3 function Convert (Item : UTF_16_Wide_String; Output_Scheme : Encoding_Scheme; Output_BOM : Boolean := False) return UTF_String; 20/3 function Convert (Item : UTF_16_Wide_String; Output_BOM : Boolean := False) return UTF_8_String; 21/3 end Ada.Strings.UTF_Encoding.Conversions; 22/5

package Ada.Strings.UTF_Encoding.Strings with Pure is 23/3 -- Encoding / decoding between String and various encoding schemes function Encode (Item : String; Output_Scheme : Encoding_Scheme; Output_BOM : Boolean := False) return UTF_String; 24/3 function Encode (Item : String; Output_BOM : Boolean := False) return UTF_8_String; 25/3 function Encode (Item : String; Output_BOM : Boolean := False) return UTF_16_Wide_String; 26/3 function Decode (Item : UTF_String; Input_Scheme : Encoding_Scheme) return String; 27/3 function Decode (Item : UTF_8_String) return String; 28/3 function Decode (Item : UTF_16_Wide_String) return String; 29/3 end Ada.Strings.UTF_Encoding.Strings; 30/5

package Ada.Strings.UTF_Encoding.Wide_Strings with Pure is 31/3 -- Encoding / decoding between Wide_String and various encoding schemes function Encode (Item : Wide_String; Output_Scheme : Encoding_Scheme; Output_BOM : Boolean := False) return UTF_String; 32/3 function Encode (Item : Wide_String; Output_BOM : Boolean := False) return UTF_8_String; 33/3 function Encode (Item : Wide_String; Output_BOM : Boolean := False) return UTF_16_Wide_String; 34/3 function Decode (Item : UTF_String; Input_Scheme : Encoding_Scheme) return Wide_String; 35/3 function Decode (Item : UTF_8_String) return Wide_String; 36/3 function Decode (Item : UTF_16_Wide_String) return Wide_String; 37/3 end Ada.Strings.UTF_Encoding.Wide_Strings; 38/5

package Ada.Strings.UTF_Encoding.Wide_Wide_Strings with Pure is 39/3 -- Encoding / decoding between Wide_Wide_String and various encoding schemes function Encode (Item : Wide_Wide_String; Output_Scheme : Encoding_Scheme; Output_BOM : Boolean := False) return UTF_String; 40/3 function Encode (Item : Wide_Wide_String; Output_BOM : Boolean := False) return UTF_8_String; 41/3 function Encode (Item : Wide_Wide_String; Output_BOM : Boolean := False) return UTF_16_Wide_String; 42/3 function Decode (Item : UTF_String; Input_Scheme : Encoding_Scheme) return Wide_Wide_String; 43/3 function Decode (Item : UTF_8_String) return Wide_Wide_String; 44/3 function Decode (Item : UTF_16_Wide_String) return Wide_Wide_String; 45/3 end Ada.Strings.UTF_Encoding.Wide_Wide_Strings;

46/3

The type Encoding_Scheme defines encoding schemes. UTF_8 corresponds to the UTF-8 encoding scheme defined by Annex D of ISO/IEC 10646. UTF_16BE corresponds to the UTF-16 encoding scheme defined by Annex C of ISO/IEC 10646 in 8 bit, big-endian order; and UTF_16LE corresponds to the UTF-16 encoding scheme in 8 bit, little-endian order.

47/3

The subtype UTF_String is used to represent a String of 8-bit values containing a sequence of values encoded in one of three ways (UTF-8, UTF-16BE, or UTF-16LE). The subtype UTF_8_String is used to represent a String of 8-bit values containing a sequence of values encoded in UTF-8. The subtype UTF_16_Wide_String is used to represent a Wide_String of 16-bit values containing a sequence of values encoded in UTF-16.

48/3

The BOM_8, BOM_16BE, BOM_16LE, and BOM_16 constants correspond to values used at the start of a string to indicate the encoding.

49/3

Each of the Encode functions takes a String, Wide_String, or Wide_Wide_String Item parameter that is assumed to be an array of unencoded characters. Each of the Convert functions takes a UTF_String, UTF_8_String, or UTF_16_String Item parameter that is assumed to contain characters whose position values correspond to a valid encoding sequence according to the encoding scheme required by the function or specified by its Input_Scheme parameter.

50/3

Each of the Convert and Encode functions returns a UTF_String, UTF_8_String, or UTF_16_String value whose characters have position values that correspond to the encoding of the Item parameter according to the encoding scheme required by the function or specified by its Output_Scheme parameter. For UTF_8, no overlong encoding is returned. A BOM is included at the start of the returned string if the Output_BOM parameter is set to True. The lower bound of the returned string is 1.

51/3

Each of the Decode functions takes a UTF_String, UTF_8_String, or UTF_16_String Item parameter which is assumed to contain characters whose position values correspond to a valid encoding sequence according to the encoding scheme required by the function or specified by its Input_Scheme parameter, and returns the corresponding String, Wide_String, or Wide_Wide_String value. The lower bound of the returned string is 1.

52/3

For each of the Convert and Decode functions, an initial BOM in the input that matches the expected encoding scheme is ignored, and a different initial BOM causes Encoding_Error to be propagated.

53/3

The exception Encoding_Error is also propagated in the following situations:

54/4
  • By a Convert or Decode function when a UTF encoded string contains an invalid encoding sequence.
54.a/4

To be honest: An overlong encoding is not invalid for the purposes of this check, and this does not depend on the character set version in use. Some recent character set standards declare overlong encodings to be invalid; it would be unnecessary and unfriendly to users for Convert or Decode to raise an exception for an overlong encoding.

55/4
  • By a Convert or Decode function when the expected encoding is UTF-16BE or UTF-16LE and the input string has an odd length.
  • 56/3
  • By a Decode function yielding a String when the decoding of a sequence results in a code point whose value exceeds 16#FF#.
  • 57/3
  • By a Decode function yielding a Wide_String when the decoding of a sequence results in a code point whose value exceeds 16#FFFF#.
  • 58/3
  • By an Encode function taking a Wide_String as input when an invalid character appears in the input. In particular, the characters whose position is in the range 16#D800# .. 16#DFFF# are invalid because they conflict with UTF-16 surrogate encodings, and the characters whose position is 16#FFFE# or 16#FFFF# are also invalid because they conflict with BOM codes.
59/3

function Encoding (Item : UTF_String; Default : Encoding_Scheme := UTF_8) return Encoding_Scheme;

60/3

Inspects a UTF_String value to determine whether it starts with a BOM for UTF-8, UTF-16BE, or UTF_16LE. If so, returns the scheme corresponding to the BOM; otherwise, returns the value of Default.

61/3

function Convert (Item : UTF_String; Input_Scheme : Encoding_Scheme; Output_Scheme : Encoding_Scheme; Output_BOM : Boolean := False) return UTF_String;

62/3

Returns the value of Item (originally encoded in UTF-8, UTF-16LE, or UTF-16BE as specified by Input_Scheme) encoded in one of these three schemes as specified by Output_Scheme.

63/3

function Convert (Item : UTF_String; Input_Scheme : Encoding_Scheme; Output_BOM : Boolean := False) return UTF_16_Wide_String;

64/3

Returns the value of Item (originally encoded in UTF-8, UTF-16LE, or UTF-16BE as specified by Input_Scheme) encoded in UTF-16.

65/3

function Convert (Item : UTF_8_String; Output_BOM : Boolean := False) return UTF_16_Wide_String;

66/3

Returns the value of Item (originally encoded in UTF-8) encoded in UTF-16.

67/3

function Convert (Item : UTF_16_Wide_String; Output_Scheme : Encoding_Scheme; Output_BOM : Boolean := False) return UTF_String;

68/3

Returns the value of Item (originally encoded in UTF-16) encoded in UTF-8, UTF-16LE, or UTF-16BE as specified by Output_Scheme.

69/3

function Convert (Item : UTF_16_Wide_String; Output_BOM : Boolean := False) return UTF_8_String;

70/3

Returns the value of Item (originally encoded in UTF-16) encoded in UTF-8.

71/3

function Encode (Item : String; Output_Scheme : Encoding_Scheme; Output_BOM : Boolean := False) return UTF_String;

72/3

Returns the value of Item encoded in UTF-8, UTF-16LE, or UTF-16BE as specified by Output_Scheme.

73/3

function Encode (Item : String; Output_BOM : Boolean := False) return UTF_8_String;

74/3

Returns the value of Item encoded in UTF-8.

75/3

function Encode (Item : String; Output_BOM : Boolean := False) return UTF_16_Wide_String;

76/3

Returns the value of Item encoded in UTF_16.

77/3

function Decode (Item : UTF_String; Input_Scheme : Encoding_Scheme) return String;

78/3

Returns the result of decoding Item, which is encoded in UTF-8, UTF-16LE, or UTF-16BE as specified by Input_Scheme.

79/3

function Decode (Item : UTF_8_String) return String;

80/3

Returns the result of decoding Item, which is encoded in UTF-8.

81/3

function Decode (Item : UTF_16_Wide_String) return String;

82/3

Returns the result of decoding Item, which is encoded in UTF-16.

83/3

function Encode (Item : Wide_String; Output_Scheme : Encoding_Scheme; Output_BOM : Boolean := False) return UTF_String;

84/3

Returns the value of Item encoded in UTF-8, UTF-16LE, or UTF-16BE as specified by Output_Scheme.

85/3

function Encode (Item : Wide_String; Output_BOM : Boolean := False) return UTF_8_String;

86/3

Returns the value of Item encoded in UTF-8.

87/3

function Encode (Item : Wide_String; Output_BOM : Boolean := False) return UTF_16_Wide_String;

88/3

Returns the value of Item encoded in UTF_16.

89/3

function Decode (Item : UTF_String; Input_Scheme : Encoding_Scheme) return Wide_String;

90/3

Returns the result of decoding Item, which is encoded in UTF-8, UTF-16LE, or UTF-16BE as specified by Input_Scheme.

91/3

function Decode (Item : UTF_8_String) return Wide_String;

92/3

Returns the result of decoding Item, which is encoded in UTF-8.

93/3

function Decode (Item : UTF_16_Wide_String) return Wide_String;

94/3

Returns the result of decoding Item, which is encoded in UTF-16.

95/3

function Encode (Item : Wide_Wide_String; Output_Scheme : Encoding_Scheme; Output_BOM : Boolean := False) return UTF_String;

96/3

Returns the value of Item encoded in UTF-8, UTF-16LE, or UTF-16BE as specified by Output_Scheme.

97/3

function Encode (Item : Wide_Wide_String; Output_BOM : Boolean := False) return UTF_8_String;

98/3

Returns the value of Item encoded in UTF-8.

99/3

function Encode (Item : Wide_Wide_String; Output_BOM : Boolean := False) return UTF_16_Wide_String;

100/3

Returns the value of Item encoded in UTF_16.

101/3

function Decode (Item : UTF_String; Input_Scheme : Encoding_Scheme) return Wide_Wide_String;

102/3

Returns the result of decoding Item, which is encoded in UTF-8, UTF-16LE, or UTF-16BE as specified by Input_Scheme.

103/3

function Decode (Item : UTF_8_String) return Wide_Wide_String;

104/3

Returns the result of decoding Item, which is encoded in UTF-8.

105/3

function Decode (Item : UTF_16_Wide_String) return Wide_Wide_String;

106/3

Returns the result of decoding Item, which is encoded in UTF-16.

Implementation Advice

107/3

If an implementation supports other encoding schemes, another similar child of Ada.Strings should be defined.

107.a.1/3
implementation advice

If an implementation supports other string encoding schemes, a child of Ada.Strings similar to UTF_Encoding should be defined.

108/3

NOTE A BOM (Byte-Order Mark, code position 16#FEFF#) can be included in a file or other entity to indicate the encoding; it is skipped when decoding. Typically, only the first line of a file or other entity contains a BOM. When decoding, the Encoding function can be called on the first line to determine the encoding; this encoding will then be used in subsequent calls to Decode to convert all of the lines to an internal format.

Extensions to Ada 2005

108.a/3

The packages Strings.UTF_Encoding, Strings.UTF_Encoding.Conversions, Strings.UTF_Encoding.Strings, Strings.UTF_Encoding.Wide_Strings, and Strings.UTF_Encoding.Wide_Wide_Strings are new.

Wording Changes from Ada 2012

108.b/4

Corrigendum: Fixed the omission that Convert routines make the same checks on input as Decode routines.

A.4.12 Universal Text Buffers

1/5

A universal text buffer can be used to save and retrieve text of any language-defined string type. The types used to save and retrieve the text can be different.

Static Semantics

2/5

The text buffer library packages have the following declarations:

3/5

with Ada.Strings.UTF_Encoding.Wide_Wide_Strings; package Ada.Strings.Text_Buffers with Pure is 4/5 type Text_Buffer_Count is range 0 .. implementation-defined; 5/5 New_Line_Count : constant Text_Buffer_Count := implementation-defined; 6/5 type Root_Buffer_Type is abstract tagged private with Default_Initial_Condition => Current_Indent (Root_Buffer_Type) = 0; 7/5 procedure Put ( Buffer : in out Root_Buffer_Type; Item : in String) is abstract; 8/5 procedure Wide_Put ( Buffer : in out Root_Buffer_Type; Item : in Wide_String) is abstract; 9/5 procedure Wide_Wide_Put ( Buffer : in out Root_Buffer_Type; Item : in Wide_Wide_String) is abstract; 10/5 procedure Put_UTF_8 ( Buffer : in out Root_Buffer_Type; Item : in UTF_Encoding.UTF_8_String) is abstract; 11/5 procedure Wide_Put_UTF_16 ( Buffer : in out Root_Buffer_Type; Item : in UTF_Encoding.UTF_16_Wide_String) is abstract; 12/5 procedure New_Line (Buffer : in out Root_Buffer_Type) is abstract; 13/5 Standard_Indent : constant Text_Buffer_Count := 3; 14/5 function Current_Indent ( Buffer : Root_Buffer_Type) return Text_Buffer_Count; 15/5 procedure Increase_Indent ( Buffer : in out Root_Buffer_Type; Amount : in Text_Buffer_Count := Standard_Indent) with Post'Class => Current_Indent (Buffer) = Current_Indent (Buffer)'Old + Amount; 16/5 procedure Decrease_Indent ( Buffer : in out Root_Buffer_Type; Amount : in Text_Buffer_Count := Standard_Indent) with Pre'Class => Current_Indent (Buffer) >= Amount or else raise Constraint_Error, Post'Class => Current_Indent (Buffer) = Current_Indent (Buffer)'Old - Amount; 17/5 private ... -- not specified by the language end Ada.Strings.Text_Buffers; 18/5

package Ada.Strings.Text_Buffers.Unbounded with Preelaborate, Nonblocking, Global => null is 19/5 type Buffer_Type is new Root_Buffer_Type with private; 20/5 function Get ( Buffer : in out Buffer_Type) return String with Post'Class => Get'Result'First = 1 and then Current_Indent (Buffer) = 0; 21/5 function Wide_Get ( Buffer : in out Buffer_Type) return Wide_String with Post'Class => Wide_Get'Result'First = 1 and then Current_Indent (Buffer) = 0; 22/5 function Wide_Wide_Get ( Buffer : in out Buffer_Type) return Wide_Wide_String with Post'Class => Wide_Wide_Get'Result'First = 1 and then Current_Indent (Buffer) = 0; 23/5 function Get_UTF_8 ( Buffer : in out Buffer_Type) return UTF_Encoding.UTF_8_String with Post'Class => Get_UTF_8'Result'First = 1 and then Current_Indent (Buffer) = 0; 24/5 function Wide_Get_UTF_16 ( Buffer : in out Buffer_Type) return UTF_Encoding.UTF_16_Wide_String with Post'Class => Wide_Get_UTF_16'Result'First = 1 and then Current_Indent (Buffer) = 0; 25/5 private ... -- not specified by the language, but will include nonabstract -- overridings of all inherited subprograms that require overriding. end Ada.Strings.Text_Buffers.Unbounded; 26/5

package Ada.Strings.Text_Buffers.Bounded with Pure, Nonblocking, Global => null is 27/5 type Buffer_Type (Max_Characters : Text_Buffer_Count) is new Root_Buffer_Type with private with Default_Initial_Condition => not Text_Truncated (Buffer_Type); 28/5 function Text_Truncated (Buffer : in Buffer_Type) return Boolean; 29/5 -- Get, Wide_Get, Wide_Wide_Get, Get_UTF_8, and Wide_Get_UTF_16 -- are declared here just as in the Unbounded child. 30/5 private ... -- not specified by the language, but will include nonabstract -- overridings of all inherited subprograms that require overriding. end Ada.Strings.Text_Buffers.Bounded;

31/5

Character_Count returns the number of characters currently stored in a text buffer.

31.a/5
ramification

This is lower-case "characters". The representation isn't considered, so it is irrelevant what type of character (Character, Wide_Character, or Wide_Wide_Character) was stored.

32/5

New_Line stores New_Line_Count characters that represent a new line into a text buffer. Current_Indent returns the current indentation associated with the buffer, with zero meaning there is no indentation in effect; Increase_Indent and Decrease_Indent increase or decrease the indentation associated with the buffer.

33/5

A call to Put, Wide_Put, Wide_Wide_Put, Put_UTF_8, or Wide_Put_UTF_16 stores a sequence of characters into the text buffer, preceded by Current_Indent(Buffer) spaces (Wide_Wide_Characters with position 32) if there is at least one character in Item and it would have been the first character on the current line.

34/5

A call to function Get, Wide_Get, Wide_Wide_Get, Get_UTF_8, or Wide_Get_UTF_16 returns the same sequence of characters as was present in the calls that stored the characters into the buffer, if representable. For a call to Get, if any character in the sequence is not defined in Character, the result is implementation defined. Similarly, for a call to Wide_Get, if any character in the sequence is not defined in Wide_Character, the result is implementation defined. As part of a call on any of the Get functions, the buffer is reset to an empty state, with no stored characters.

34.a/5
implementation defined

The value returned by a call to a Text_Buffer Get procedure if any character in the returned sequence is not defined in Character.

34.b/5
implementation defined

The value returned by a call to a Text_Buffer Wide_Get procedure if any character in the returned sequence is not defined in Wide_Character.

35/5

In the case of a Buf of type Text_Buffers.Bounded.Buffer_Type, Text_Truncated (Buf) returns True if the various Put procedures together have attempted to store more than Buf.Max_Characters into Buf. If this function returns True, then the various Get functions return a representation of only the first Buf.Max_Characters characters that were stored in Buf.

Implementation Advice

36/5

Bounded buffer objects should be implemented without dynamic allocation.

36.a.1/5
implementation advice

Bounded buffer objects should be implemented without dynamic allocation.

Extensions to Ada 2012

36.a/5

The packages Strings.Text_Buffers, Strings.Text_Buffers.Unbounded, and Strings.Text_Buffers.Bounded are new.