1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
#############################################################################
##
## This file is part of GAP, a system for computational discrete algebra.
## This file's authors include Thomas Breuer, Frank Celler.
##
## Copyright of GAP belongs to its developers, whose names are too numerous
## to list here. Please refer to the COPYRIGHT file for details.
##
## SPDX-License-Identifier: GPL-2.0-or-later
##
## This file deals with strings and characters.
##
#############################################################################
##
#C IsChar( <obj> ) . . . . . . . . . . . . . . . . . category of characters
#C IsCharCollection( <obj> ) . . . . . category of collections of characters
##
## <#GAPDoc Label="IsChar">
## <ManSection>
## <Filt Name="IsChar" Arg='obj' Type='Category'/>
## <Filt Name="IsCharCollection" Arg='obj' Type='Category'/>
##
## <Description>
## A <E>character</E> is simply an object in &GAP; that represents an
## arbitrary character from the character set of the operating system.
## Character literals can be entered in &GAP; by enclosing the character
## in <E>singlequotes</E> <C>'</C>.
## <Example><![CDATA[
## gap> x:= 'a'; IsChar( x );
## 'a'
## true
## gap> '*';
## '*'
## ]]></Example>
## </Description>
## </ManSection>
## <#/GAPDoc>
##
DeclareCategory( "IsChar", IS_OBJECT );
DeclareCategoryCollections( "IsChar" );
#############################################################################
##
#C IsString( <obj> ) . . . . . . . . . . . . . . . . . . category of strings
##
## <#GAPDoc Label="IsString">
## <ManSection>
## <Filt Name="IsString" Arg='obj'/>
##
## <Description>
## A <E>string</E> is a dense list (see <Ref Filt="IsList"/>,
## <Ref Filt="IsDenseList"/>) of characters (see <Ref Filt="IsChar"/>);
## thus strings are always homogeneous
## (see <Ref Filt="IsHomogeneousList"/>).
## <P/>
## A string literal can either be entered as the list of characters
## or by writing the characters between <E>doublequotes</E> <C>"</C>.
## &GAP; will always output strings in the latter format.
## However, the input via the double quote syntax enables &GAP; to store
## the string in an efficient compact internal representation.
## See <Ref Filt="IsStringRep"/> below for more details.
## <P/>
## Each character, in particular those which cannot be typed directly from
## the keyboard, can also be typed in three digit octal notation, or
## two digit hexadecimal notation.
## And for some special characters (like the newline character) there is a
## further possibility to type them,
## see section <Ref Sect="Special Characters"/>.
## <P/>
## <Example><![CDATA[
## gap> s1 := ['H','e','l','l','o',' ','w','o','r','l','d','.'];
## "Hello world."
## gap> IsString( s1 );
## true
## gap> s2 := "Hello world.";
## "Hello world."
## gap> s1 = s2;
## true
## gap> s3 := ""; # the empty string
## ""
## gap> s3 = [];
## true
## gap> IsString( [] );
## true
## gap> IsString( "123" ); IsString( 123 );
## true
## false
## gap> IsString( [ '1', '2', '3' ] );
## true
## gap> IsString( [ '1', '2', , '4' ] ); # strings must be dense
## false
## gap> IsString( [ '1', '2', 3 ] ); # strings must only contain characters
## false
## ]]></Example>
## </Description>
## </ManSection>
## <#/GAPDoc>
##
DeclareCategoryKernel( "IsString", IsHomogeneousList, IS_STRING );
InstallTrueMethod( IsString, IsCharCollection and IsList );
#############################################################################
##
#R IsStringRep( <obj> )
##
## <#GAPDoc Label="IsStringRep">
## <ManSection>
## <Filt Name="IsStringRep" Arg='obj' Type='Representation'/>
##
## <Description>
## <Ref Filt="IsStringRep"/> is a special (internal) representation of dense
## lists of characters.
## Dense lists of characters can be converted into this representation
## using <Ref Func="ConvertToStringRep"/>.
## Note that calling <Ref Filt="IsString"/> does <E>not</E> change the
## representation.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
DeclareRepresentationKernel( "IsStringRep",
IsInternalRep, IS_STRING_REP );
#############################################################################
##
#F ConvertToStringRep( <obj> ) . . . . . . . . . . . . . inplace conversion
##
## <#GAPDoc Label="ConvertToStringRep">
## <ManSection>
## <Func Name="ConvertToStringRep" Arg='obj'/>
##
## <Description>
## If <A>obj</A> is a dense internally represented list of characters then
## <Ref Func="ConvertToStringRep"/> changes the representation to
## <Ref Filt="IsStringRep"/>.
## This is useful in particular for converting the empty list <C>[]</C>,
## which usually is in <Ref Filt="IsPlistRep"/>,
## to <Ref Filt="IsStringRep"/>.
## If <A>obj</A> is not a string then <Ref Func="ConvertToStringRep"/>
## signals an error.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
BIND_GLOBAL( "ConvertToStringRep", CONV_STRING );
#############################################################################
##
#F CopyToStringRep( <obj> ) . . . . . . . . . . . . . . . copy conversion
##
## <#GAPDoc Label="CopyToStringRep">
## <ManSection>
## <Func Name="CopyToStringRep" Arg='obj'/>
##
## <Description>
## If <A>obj</A> is a dense internally represented list of characters then
## <Ref Func="CopyToStringRep"/> copies <A>obj</A> to a new object with
## representation
## <Ref Filt="IsStringRep"/>.
## If <A>obj</A> is not a string then <Ref Func="CopyToStringRep"/>
## signals an error.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
BIND_GLOBAL( "CopyToStringRep", COPY_TO_STRING_REP );
#############################################################################
##
#V CharsFamily . . . . . . . . . . . . . . . . . . . . family of characters
##
## <#GAPDoc Label="CharsFamily">
## <ManSection>
## <Fam Name="CharsFamily"/>
##
## <Description>
## Each character lies in the family <Ref Fam="CharsFamily"/>,
## each nonempty string lies in the collections family of this family.
## Note the subtle differences between the empty list <C>[]</C> and the
## empty string <C>""</C> when both are printed.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
BIND_GLOBAL( "CharsFamily", NewFamily( "CharsFamily", IsChar ) );
#############################################################################
##
#V TYPE_CHAR . . . . . . . . . . . . . . . . . . . . . . type of a character
##
## <ManSection>
## <Var Name="TYPE_CHAR"/>
##
## <Description>
## </Description>
## </ManSection>
##
BIND_GLOBAL( "TYPE_CHAR", NewType( CharsFamily, IsChar and IsInternalRep ) );
#############################################################################
##
## Types and family for strings
##
BIND_GLOBAL( "StringFamily", NewFamily( "StringsFamily", IsCharCollection ) );
BIND_GLOBAL( "TYPE_STRING_MUTABLE",
NewType( StringFamily, IsString and IsStringRep and
IsMutable ) );
BIND_GLOBAL( "TYPE_STRING_IMMUTABLE",
NewType( StringFamily, IsString and IsStringRep ) );
BIND_GLOBAL( "TYPE_STRING_NSORT_MUTABLE",
NewType (StringFamily, IsString and IsStringRep and
HasIsSSortedList and IsMutable ) );
BIND_GLOBAL( "TYPE_STRING_NSORT_IMMUTABLE",
NewType (StringFamily, IsString and IsStringRep and
HasIsSSortedList ) );
BIND_GLOBAL( "TYPE_STRING_SSORT_MUTABLE",
NewType (StringFamily, IsString and IsStringRep and
IsSSortedList and IsMutable ) );
BIND_GLOBAL( "TYPE_STRING_SSORT_IMMUTABLE",
NewType (StringFamily, IsString and IsStringRep and
IsSSortedList ) );
#############################################################################
##
#F IsEmptyString( <str> ) . . . . . . . . . . . . . . . empty string tester
##
## <#GAPDoc Label="IsEmptyString">
## <ManSection>
## <Func Name="IsEmptyString" Arg='str'/>
##
## <Description>
## <Ref Func="IsEmptyString"/> returns <K>true</K> if <A>str</A> is the
## empty string in the representation <Ref Filt="IsStringRep"/>,
## and <K>false</K> otherwise.
## Note that the empty list <C>[]</C> and the empty string <C>""</C> have
## the same type, the recommended way to distinguish them is via
## <Ref Func="IsEmptyString"/>.
## For formatted printing, this distinction is sometimes necessary.
## <!-- The type is the same because <C>IsStringRep</C> is not <E>set</E> in this type,-->
## <!-- and <C>IsPlistRep</C> is <E>set</E>,-->
## <!-- although <E>calling</E> <C>IsStringRep</C> for <C>[]</C> yields <K>false</K>,-->
## <!-- and <E>calling</E> <C>IsPlistRep</C> for <C>""</C> yields <K>false</K>, too.-->
## <P/>
## <Example><![CDATA[
## gap> l:= [];; IsString( l ); IsEmptyString( l ); IsEmpty( l );
## true
## false
## true
## gap> l; ConvertToStringRep( l ); l;
## [ ]
## ""
## gap> IsEmptyString( l ); IsEmptyString( "" ); IsEmptyString( "abc" );
## true
## true
## false
## gap> ll:= [ 'a', 'b' ]; IsStringRep( ll ); ConvertToStringRep( ll );
## "ab"
## false
## gap> ll; IsStringRep( ll );
## "ab"
## true
## ]]></Example>
## </Description>
## </ManSection>
## <#/GAPDoc>
##
BIND_GLOBAL( "IsEmptyString",
obj -> IsString( obj )
and IsEmpty( obj )
and IsStringRep( obj ) );
############################################################################
##
#M String( <str> ) . . . . . . . . . . . . . . . . . . . . . . for a string
##
InstallMethod( String,
"for a string (do nothing)",
[ IsString ],
function(s)
if Length(s) = 0 and not IsStringRep(s) then
return "[ ]";
else
return s;
fi;
end);
############################################################################
##
#M String( <str> ) . . . . . . . . . . . . . . . . . . . . for a character
##
InstallMethod( String,
"for a character",
[ IsChar ],
function(ch)
local res; res := "\'"; Add(res, ch); Add(res, '\''); return res;
end);
#############################################################################
##
#F UserHomeExpand( <str> ) . . . . . . . . . . . . expand leading ~ in str
##
## <#GAPDoc Label="UserHomeExpand">
## <ManSection>
## <Func Name="UserHomeExpand" Arg='str'/>
## <Description>
## If the string <A>str</A> starts with a <C>'~'</C> character this
## function returns a new string with the leading <C>'~'</C> substituted by
## the user's home directory as stored in <C>GAPInfo.UserHome</C>.
## Otherwise <A>str</A> is returned unchanged.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
BIND_GLOBAL("UserHomeExpand", function(str)
if IsString(str) and Length(str) > 0 and str[1] = '~'
and IsString(GAPInfo.UserHome) and Length( GAPInfo.UserHome ) > 0 then
return Concatenation( GAPInfo.UserHome, str{[2..Length(str)]});
else
return str;
fi;
end);
# the character set definitions might be needed when processing files, thus
# they must come earlier.
BIND_GLOBAL("CHARS_DIGITS",MakeImmutable(LIST_SORTED_LIST("0123456789")));
BIND_GLOBAL("CHARS_UALPHA",
MakeImmutable(LIST_SORTED_LIST("ABCDEFGHIJKLMNOPQRSTUVWXYZ")));
BIND_GLOBAL("CHARS_LALPHA",
MakeImmutable(LIST_SORTED_LIST("abcdefghijklmnopqrstuvwxyz")));
BIND_GLOBAL("CHARS_ALPHA",
MakeImmutable(LIST_SORTED_LIST("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")));
BIND_GLOBAL("CHARS_SYMBOLS",
MakeImmutable(LIST_SORTED_LIST(" !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")));
#############################################################################
##
#F _StripEscapeSequences( <str> ) . . . . . remove escape sequences from str
##
## <#GAPDoc Label="_StripEscapeSequences">
## <ManSection >
## <Func Arg="str" Name="_StripEscapeSequences" />
## <Returns>string without escape sequences</Returns>
## <Description>
## This function returns the string one gets from the string <A>str</A> by
## removing all escape sequences. If <A>str</A> does not contain such a
## sequence then <A>str</A> itself is returned.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
BIND_GLOBAL("_StripEscapeSequences", function(str)
local esc, res, i, ls, p;
esc := CHAR_INT(27);
res := "";
i := 1;
ls := Length(str);
while i <= ls do
if str[i] = esc then
i := i+1;
while not str[i] in CHARS_ALPHA do
i := i+1;
od;
# first letter is last character of escape sequence
i := i+1;
# remove \027 marker of inner escape sequences as well
if IsBound(str[i]) and str[i] = '\027' then
i := i+1;
fi;
else
p := Position(str, esc, i);
if p=fail then
if i=1 then
# don't copy if no escape there
return str;
else
Append(res, str{[i..ls]});
return res;
fi;
else
Append(res, str{[i..p-1]});
i := p;
fi;
fi;
od;
return res;
end);
|