File: string.g

package info (click to toggle)
gap 4r4p4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 25,972 kB
  • ctags: 6,672
  • sloc: ansic: 95,121; sh: 3,137; makefile: 219; perl: 11; awk: 6
file content (190 lines) | stat: -rw-r--r-- 6,859 bytes parent folder | download | duplicates (4)
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
#############################################################################
##
#W  string.g                     GAP library                    Thomas Breuer
#W                                                             & Frank Celler
##
#H  @(#)$Id: string.g,v 4.20.4.1 2004/02/03 11:59:07 gap Exp $
##
#Y  Copyright (C)  1997,  Lehrstuhl D fuer Mathematik,  RWTH Aachen,  Germany
#Y  (C) 1998 School Math and Comp. Sci., University of St.  Andrews, Scotland
#Y  Copyright (C) 2002 The GAP Group
##
##  This file deals with strings and characters.
##
Revision.string_g :=
    "@(#)$Id: string.g,v 4.20.4.1 2004/02/03 11:59:07 gap Exp $";


#############################################################################
##
#C  IsChar( <obj> ) . . . . . . . . . . . . . . . . .  category of characters
#C  IsCharCollection( <obj> ) . . . . . category of collections of characters
##
##  A *character* 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 *singlequotes* `{'}'.
##
DeclareCategory( "IsChar", IS_OBJECT );

DeclareCategoryCollections( "IsChar" );


#############################################################################
##
#C  IsString( <obj> ) . . . . . . . . . . . . . . . . . . category of strings
##
##  A *string* is  a dense list (see~"IsList", "IsDenseList")
##  of characters (see~"IsChar"); thus strings are always homogeneous
##  (see~"IsHomogeneousList").
##  
##  A string literal can either be entered  as the list of characters
##  or by writing the characters between *doublequotes* `\"'.
##  {\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
##  "IsStringRep" below for more details.
##  
##  Each character, in particular those which cannot be typed directly from the
##  keyboard, can also be typed in three digit octal notation. And for some
##  special characters (like the newline character) there is a further
##  possibility to type them, see section "Special Characters".
##
DeclareCategoryKernel( "IsString", IsHomogeneousList, IS_STRING );

InstallTrueMethod( IsString, IsCharCollection and IsList );


#############################################################################
##
#R  IsStringRep( <obj> )
##
##  `IsStringRep' is a special (internal) representation of dense lists
##  of characters.
##  Dense lists of characters can be converted into this representation
##  using `ConvertToStringRep'.
##  Note that calling `IsString' does *not* change the representation.
##
DeclareRepresentationKernel( "IsStringRep",
    IsInternalRep, [], IS_OBJECT, IS_STRING_REP );


#############################################################################
##
#F  ConvertToStringRep( <obj> ) . . . . . . . . . . . . .  inplace conversion
##
##  If <obj> is a dense internally represented list of characters then
##  `ConvertToStringRep' changes the representation to `IsStringRep'.
##  This is useful in particular for converting the empty list `[]',
##  which usually is in `IsPlistRep', to `IsStringRep'.
##  If <obj> is not a string then `ConvertToStringRep' signals an error.
##
BIND_GLOBAL( "ConvertToStringRep", CONV_STRING );


#############################################################################
##
#V  CharsFamily . . . . . . . . . . . . . . . . . . . .  family of characters
##
##  Each character lies in the family `CharFamily',
##  each nonempty string lies in the collections family of this family.
##  Note the subtle differences between the empty list `[]' and the empty
##  string `\"\"' when both are printed.
##
BIND_GLOBAL( "CharsFamily", NewFamily( "CharsFamily", IsChar ) );


#############################################################################
##
#V  TYPE_CHAR . . . . . . . . . . . . . . . . . . . . . . type of a character
##
BIND_GLOBAL( "TYPE_CHAR", NewType( CharsFamily, IsChar and IsInternalRep ) );

#############################################################################
##
#V  TYPES_STRING . . . . . . . . . . . . . . . . . . . . . types of strings
##
BIND_GLOBAL( "StringFamily", NewFamily( "StringsFamily", IsCharCollection ) );

BIND_GLOBAL( "TYPES_STRING", 
        [ NewType( StringFamily, IsString and IsStringRep and
                IsMutable ), # T_STRING
          
          NewType( StringFamily, IsString and IsStringRep ), 
          # T_STRING + IMMUTABLE
          
          ~[1], # T_STRING_NSORT
          
          ~[2], # T_STRING_NSORT + IMMUTABLE
          
          NewType (StringFamily, IsString and IsStringRep and
                  IsSSortedList and IsMutable ),
          # T_STRING_SSORT 
          
          NewType (StringFamily, IsString and IsStringRep and
                  IsSSortedList )
          # T_STRING_SSORT +IMMUTABLE
          ]);


          



#############################################################################
##
#F  IsEmptyString( <str> )  . . . . . . . . . . . . . . . empty string tester
##
##  `IsEmptyString' returns `true' if <str> is the empty string in the
##  representation `IsStringRep', and `false' otherwise.
##  Note that the empty list `[]' and the empty string `\"\"' have the same
##  type, the recommended way to distinguish them is via `IsEmptyString'.
##  For formatted printing, this distinction is sometimes necessary.
#T The type is the same because `IsStringRep' is not *set* in this type,
#T and `IsPlistRep' is *set*,
#T although *calling* `IsStringRep' for `[]' yields `false',
#T and *calling* `IsPlistRep' for `\"\"' yields `false', too.
#T Why is `TNUM_OBJ_INT' used here,
#T calling `IsStringRep' would be enough, or?
##
BIND_GLOBAL( "TNUM_EMPTY_STRING",
             [ TNUM_OBJ_INT( "" ), TNUM_OBJ_INT( Immutable( "" ) ) ] );

BIND_GLOBAL( "IsEmptyString",
    obj ->     IsString( obj )
           and IsEmpty( obj )
           and TNUM_OBJ_INT( obj ) in TNUM_EMPTY_STRING );


############################################################################
##
#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);


#############################################################################
##
#E