1 (* Strings.mod provides a Logitech-3.0 compatible library.
3 Copyright (C) 2005-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
27 IMPLEMENTATION MODULE Strings ;
29 FROM ASCII IMPORT nul ;
31 IMPORT DynamicStrings ;
35 Assign - source := dest.
38 PROCEDURE Assign (VAR dest: ARRAY OF CHAR; source: ARRAY OF CHAR) ;
40 StrLib.StrCopy(source, dest)
45 Insert - insert the string, substr, into str at position, index.
46 substr, is added to the end of, str, if, index >= length(str)
49 PROCEDURE Insert (substr: ARRAY OF CHAR; VAR str: ARRAY OF CHAR;
52 s1, s2: DynamicStrings.String ;
56 ConCat(str, substr, str)
58 s1 := DynamicStrings.InitString(str) ;
59 s2 := DynamicStrings.ConCat(DynamicStrings.Slice(s1, 0, index),
60 DynamicStrings.Mark(DynamicStrings.InitString(str))) ;
61 s2 := DynamicStrings.ConCat(s2, DynamicStrings.Slice(s1, index, 0)) ;
62 DynamicStrings.CopyOut(str, s2) ;
63 s1 := DynamicStrings.KillString(s1) ;
64 s2 := DynamicStrings.KillString(s2)
70 Delete - delete len characters from, str, starting at, index.
73 PROCEDURE Delete (VAR str: ARRAY OF CHAR; index: CARDINAL; length: CARDINAL) ;
75 s: DynamicStrings.String ;
77 s := DynamicStrings.InitString(str) ;
78 s := DynamicStrings.ConCat(DynamicStrings.Mark(DynamicStrings.Slice(s, 0, index)),
79 DynamicStrings.Mark(DynamicStrings.Slice(s, index+length, 0))) ;
80 DynamicStrings.CopyOut(str, s) ;
81 s := DynamicStrings.KillString(s)
86 Pos - return the first position of, substr, in, str.
89 PROCEDURE Pos (substr, str: ARRAY OF CHAR) : CARDINAL ;
92 s1, s2, s3: DynamicStrings.String ;
94 s1 := DynamicStrings.InitString(str) ;
95 s2 := DynamicStrings.InitString(substr) ;
96 k := DynamicStrings.Length(s1) ;
97 l := DynamicStrings.Length(s2) ;
100 i := DynamicStrings.Index(s1, DynamicStrings.char(s2, 0), i) ;
103 s3 := DynamicStrings.Slice(s1, i, l) ;
104 IF DynamicStrings.Equal(s3, s2)
106 s1 := DynamicStrings.KillString(s1) ;
107 s2 := DynamicStrings.KillString(s2) ;
108 s3 := DynamicStrings.KillString(s3) ;
111 s3 := DynamicStrings.KillString(s3)
115 s1 := DynamicStrings.KillString(s1) ;
116 s2 := DynamicStrings.KillString(s2) ;
117 s3 := DynamicStrings.KillString(s3) ;
118 RETURN( HIGH(str)+1 )
123 Copy - copy at most, length, characters in, substr, to, str,
124 starting at position, index.
127 PROCEDURE Copy (str: ARRAY OF CHAR;
128 index, length: CARDINAL; VAR result: ARRAY OF CHAR) ;
130 s1, s2: DynamicStrings.String ;
132 s1 := DynamicStrings.InitString(str) ;
133 s2 := DynamicStrings.Slice(s1, index, index+length) ;
134 DynamicStrings.CopyOut(result, s2) ;
135 s1 := DynamicStrings.KillString(s1) ;
136 s2 := DynamicStrings.KillString(s2)
141 ConCat - concatenates two strings, s1, and, s2
142 and places the result into, dest.
145 PROCEDURE ConCat (s1, s2: ARRAY OF CHAR; VAR dest: ARRAY OF CHAR) ;
147 StrLib.StrConCat(s1, s2, dest)
152 Length - return the length of string, s.
155 PROCEDURE Length (s: ARRAY OF CHAR) : CARDINAL ;
157 RETURN( StrLib.StrLen(s) )
162 CompareStr - compare two strings, left, and, right.
165 PROCEDURE CompareStr (left, right: ARRAY OF CHAR) : INTEGER ;
167 IF StrLib.StrLess(left, right)
170 ELSIF StrLib.StrEqual(left, right)