File: chap6.html

package info (click to toggle)
gap-gapdoc 1.6.7-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,596 kB
  • sloc: xml: 3,502; makefile: 244; javascript: 155; sh: 3
file content (595 lines) | stat: -rw-r--r-- 53,004 bytes parent folder | download
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>GAP (GAPDoc) - Chapter 6: String and Text Utilities</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="generator" content="GAPDoc2HTML" />
<link rel="stylesheet" type="text/css" href="manual.css" />
<script src="manual.js" type="text/javascript"></script>
<script type="text/javascript">overwriteStyle();</script>
</head>
<body class="chap6"  onload="jscontent()">


<div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a>  <a href="chap1.html">1</a>  <a href="chap2.html">2</a>  <a href="chap3.html">3</a>  <a href="chap4.html">4</a>  <a href="chap5.html">5</a>  <a href="chap6.html">6</a>  <a href="chap7.html">7</a>  <a href="chapA.html">A</a>  <a href="chapB.html">B</a>  <a href="chapC.html">C</a>  <a href="chapBib.html">Bib</a>  <a href="chapInd.html">Ind</a>  </div>

<div class="chlinkprevnexttop">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp;  <a href="chap0.html#contents">[Contents]</a>&nbsp;  &nbsp;<a href="chap5.html">[Previous Chapter]</a>&nbsp;  &nbsp;<a href="chap7.html">[Next Chapter]</a>&nbsp;  </div>

<p id="mathjaxlink" class="pcenter"><a href="chap6_mj.html">[MathJax on]</a></p>
<p><a id="X86CEF540862EE042" name="X86CEF540862EE042"></a></p>
<div class="ChapSects"><a href="chap6.html#X86CEF540862EE042">6 <span class="Heading">String and Text Utilities</span></a>
<div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap6.html#X847DA07C7C46B38A">6.1 <span class="Heading">Text Utilities</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X786D477C7AB636AA">6.1-1 WHITESPACE</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X785F61E77899580E">6.1-2 TextAttr</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X7B8AD7517E5FD0EA">6.1-3 WrapTextAttribute</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X812058CE7C8E9022">6.1-4 FormatParagraph</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X82A9121678923445">6.1-5 SubstitutionSublist</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X83DE31017B557136">6.1-6 StripBeginEnd</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X7A5978CF84C3C2D3">6.1-7 StripEscapeSequences</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X7D71CB837EE969D4">6.1-8 RepeatedString</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X7CEEA5B57D7BB38F">6.1-9 NumberDigits</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X79EF038284598D41">6.1-10 LabelInt</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X7AF694D9839BF65C">6.1-11 PositionMatchingDelimiter</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X832556617F10AAA8">6.1-12 WordsString</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X83F2821783DA9826">6.1-13 Base64String</a></span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap6.html#X8489C67D80399814">6.2 <span class="Heading">Unicode Strings</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X8475671278948DDD">6.2-1 <span class="Heading">Unicode Strings and Characters</span></a>
</span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X818A31567EB30A39">6.2-2 Encode</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X801237207E06A876">6.2-3 <span class="Heading">Lengths of UTF-8 strings</span></a>
</span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X7E2974CD84977819">6.2-4 InitialSubstringUTF8String</a></span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap6.html#X860C83047DC4F1BC">6.3 <span class="Heading">Print Utilities</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X8603B90C7C3F0AB1">6.3-1 PrintTo1</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X829B720C86E57E8B">6.3-2 StringPrint</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X812A8326844BC910">6.3-3 PrintFormattedString</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X7BB6731F7E3AAA98">6.3-4 Page</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap6.html#X7E14D32181FBC3C3">6.3-5 StringFile</a></span>
</div></div>
</div>

<h3>6 <span class="Heading">String and Text Utilities</span></h3>

<p><a id="X847DA07C7C46B38A" name="X847DA07C7C46B38A"></a></p>

<h4>6.1 <span class="Heading">Text Utilities</span></h4>

<p>This section describes some utility functions for handling texts within <strong class="pkg">GAP</strong>. They are used by the functions in the <strong class="pkg">GAPDoc</strong> package but may be useful for other purposes as well. We start with some variables containing useful strings and go on with functions for parsing and reformatting text.</p>

<p><a id="X786D477C7AB636AA" name="X786D477C7AB636AA"></a></p>

<h5>6.1-1 WHITESPACE</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; WHITESPACE</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CAPITALLETTERS</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SMALLLETTERS</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; LETTERS</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; DIGITS</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; HEXDIGITS</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; BOXCHARS</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
<p>These variables contain sets of characters which are useful for text processing. They are defined as follows.</p>


<dl>
<dt><strong class="Mark"><code class="code">WHITESPACE</code></strong></dt>
<dd><p><code class="code">" \n\t\r"</code></p>

</dd>
<dt><strong class="Mark"><code class="code">CAPITALLETTERS</code></strong></dt>
<dd><p><code class="code">"ABCDEFGHIJKLMNOPQRSTUVWXYZ"</code></p>

</dd>
<dt><strong class="Mark"><code class="code">SMALLLETTERS</code></strong></dt>
<dd><p><code class="code">"abcdefghijklmnopqrstuvwxyz"</code></p>

</dd>
<dt><strong class="Mark"><code class="code">LETTERS</code></strong></dt>
<dd><p>concatenation of <code class="code">CAPITALLETTERS</code> and <code class="code">SMALLLETTERS</code></p>

</dd>
<dt><strong class="Mark"><code class="code">DIGITS</code></strong></dt>
<dd><p><code class="code">"0123456789"</code></p>

</dd>
<dt><strong class="Mark"><code class="code">HEXDIGITS</code></strong></dt>
<dd><p><code class="code">"0123456789ABCDEFabcdef"</code></p>

</dd>
<dt><strong class="Mark"><code class="code">BOXCHARS</code></strong></dt>
<dd><p><code class="code">"─│┌┬┐├┼┤└┴┘━┃┏┳┓┣╋┫┗┻┛═║╔╦╗╠╬╣╚╩╝"</code> , these are in UTF-8 encoding, the <code class="code">i</code>-th unicode character is <code class="code">BOXCHARS{[3*i-2..3*i]}</code>.</p>

</dd>
</dl>
<p><a id="X785F61E77899580E" name="X785F61E77899580E"></a></p>

<h5>6.1-2 TextAttr</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; TextAttr</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
<p>The record <code class="func">TextAttr</code> contains strings which can be printed to change the terminal attribute for the following characters. This only works with terminals which understand basic ANSI escape sequences. Try the following example to see if this is the case for the terminal you are using. It shows the effect of the foreground and background color attributes and of the <code class="code">.bold</code>, <code class="code">.blink</code>, <code class="code">.normal</code>, <code class="code">.reverse</code> and <code class="code">.underscore</code> which can partly be mixed.</p>


<div class="example"><pre>
extra := ["CSI", "reset", "delline", "home"];;
for t in Difference(RecNames(TextAttr), extra) do
  Print(TextAttr.(t), "TextAttr.", t, TextAttr.reset,"\n");
od;
</pre></div>

<p>The suggested defaults for colors <code class="code">0..7</code> are black, red, green, brown, blue, magenta, cyan, white. But this may be different for your terminal configuration.</p>

<p>The escape sequence <code class="code">.delline</code> deletes the content of the current line and <code class="code">.home</code> moves the cursor to the beginning of the current line.</p>


<div class="example"><pre>
for i in [1..5] do 
  Print(TextAttr.home, TextAttr.delline, String(i,-6), "\c"); 
  Sleep(1); 
od;
</pre></div>

<p>Whenever you use this in some printing routines you should make it optional. Use these attributes only when <code class="code">UserPreference("UseColorsInTerminal");</code> returns <code class="keyw">true</code>.</p>

<p><a id="X7B8AD7517E5FD0EA" name="X7B8AD7517E5FD0EA"></a></p>

<h5>6.1-3 WrapTextAttribute</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; WrapTextAttribute</code>( <var class="Arg">str</var>, <var class="Arg">attr</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: a string with markup</p>

<p>The argument <var class="Arg">str</var> must be a text as <strong class="pkg">GAP</strong> string, possibly with markup by escape sequences as in <code class="func">TextAttr</code> (<a href="chap6.html#X785F61E77899580E"><span class="RefLink">6.1-2</span></a>). This function returns a string which is wrapped by the escape sequences <var class="Arg">attr</var> and <code class="code">TextAttr.reset</code>. It takes care of markup in the given string by appending <var class="Arg">attr</var> also after each given <code class="code">TextAttr.reset</code> in <var class="Arg">str</var>.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">str := Concatenation("XXX",TextAttr.2, "BLUB", TextAttr.reset,"YYY");</span>
"XXX\033[32mBLUB\033[0mYYY"
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">str2 := WrapTextAttribute(str, TextAttr.1);</span>
"\033[31mXXX\033[32mBLUB\033[0m\033[31m\027YYY\033[0m"
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">str3 := WrapTextAttribute(str, TextAttr.underscore);</span>
"\033[4mXXX\033[32mBLUB\033[0m\033[4m\027YYY\033[0m"
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># use Print(str); and so on to see how it looks like.</span>
</pre></div>

<p><a id="X812058CE7C8E9022" name="X812058CE7C8E9022"></a></p>

<h5>6.1-4 FormatParagraph</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; FormatParagraph</code>( <var class="Arg">str</var>[, <var class="Arg">len</var>][, <var class="Arg">flush</var>][, <var class="Arg">attr</var>][, <var class="Arg">widthfun</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: the formatted paragraph as string</p>

<p>This function formats a text given in the string <var class="Arg">str</var> as a paragraph. The optional arguments have the following meaning:</p>


<dl>
<dt><strong class="Mark"><var class="Arg">len</var></strong></dt>
<dd><p>the length of the lines of the formatted text, default is <code class="code">78</code> (counted without a visible length of the strings specified in the <var class="Arg">attr</var> argument)</p>

</dd>
<dt><strong class="Mark"><var class="Arg">flush</var></strong></dt>
<dd><p>can be <code class="code">"left"</code>, <code class="code">"right"</code>, <code class="code">"center"</code> or <code class="code">"both"</code>, telling that lines should be flushed left, flushed right, centered or left-right justified, respectively, default is <code class="code">"both"</code></p>

</dd>
<dt><strong class="Mark"><var class="Arg">attr</var></strong></dt>
<dd><p>is a list of two strings; the first is prepended and the second appended to each line of the result (can for example be used for indenting, <code class="code">[" ", ""]</code>, or some markup, <code class="code">[TextAttr.bold, TextAttr.reset]</code>, default is <code class="code">["", ""]</code>)</p>

</dd>
<dt><strong class="Mark"><var class="Arg">widthfun</var></strong></dt>
<dd><p>must be a function which returns the display width of text in <var class="Arg">str</var>. The default is <code class="code">Length</code> assuming that each byte corresponds to a character of width one. If <var class="Arg">str</var> is given in <code class="code">UTF-8</code> encoding one can use <code class="func">WidthUTF8String</code> (<a href="chap6.html#X801237207E06A876"><span class="RefLink">6.2-3</span></a>) here.</p>

</dd>
</dl>
<p>This function tries to handle markup with the escape sequences explained in <code class="func">TextAttr</code> (<a href="chap6.html#X785F61E77899580E"><span class="RefLink">6.1-2</span></a>) correctly.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">str := "One two three four five six seven eight nine ten eleven.";;</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Print(FormatParagraph(str, 25, "left", ["/* ", " */"]));           </span>
/* One two three four five */
/* six seven eight nine ten */
/* eleven. */
</pre></div>

<p><a id="X82A9121678923445" name="X82A9121678923445"></a></p>

<h5>6.1-5 SubstitutionSublist</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SubstitutionSublist</code>( <var class="Arg">list</var>, <var class="Arg">sublist</var>, <var class="Arg">new</var>[, <var class="Arg">flag</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: the changed list</p>

<p>This function looks for (non-overlapping) occurrences of a sublist <var class="Arg">sublist</var> in a list <var class="Arg">list</var> (compare <code class="func">PositionSublist</code> (<a href="../../../doc/ref/chap21.html#X87A8C62A867D6DA4"><span class="RefLink">Reference: PositionSublist</span></a>)) and returns a list where these are substituted with the list <var class="Arg">new</var>.</p>

<p>The optional argument <var class="Arg">flag</var> can either be <code class="code">"all"</code> (this is the default if not given) or <code class="code">"one"</code>. In the second case only the first occurrence of <var class="Arg">sublist</var> is substituted.</p>

<p>If <var class="Arg">sublist</var> does not occur in <var class="Arg">list</var> then <var class="Arg">list</var> itself is returned (and not a <code class="code">ShallowCopy(list)</code>).</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SubstitutionSublist("xababx", "ab", "a");</span>
"xaax"
</pre></div>

<p><a id="X83DE31017B557136" name="X83DE31017B557136"></a></p>

<h5>6.1-6 StripBeginEnd</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; StripBeginEnd</code>( <var class="Arg">list</var>, <var class="Arg">strip</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: changed string</p>

<p>Here <var class="Arg">list</var> and <var class="Arg">strip</var> must be lists. This function returns the sublist of list which does not contain the leading and trailing entries which are entries of <var class="Arg">strip</var>. If the result is equal to <var class="Arg">list</var> then <var class="Arg">list</var> itself is returned.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">StripBeginEnd(" ,a, b,c,   ", ", ");</span>
"a, b,c"
</pre></div>

<p><a id="X7A5978CF84C3C2D3" name="X7A5978CF84C3C2D3"></a></p>

<h5>6.1-7 StripEscapeSequences</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; StripEscapeSequences</code>( <var class="Arg">str</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: string without escape sequences</p>

<p>This function returns the string one gets from the string <var class="Arg">str</var> by removing all escape sequences which are explained in <code class="func">TextAttr</code> (<a href="chap6.html#X785F61E77899580E"><span class="RefLink">6.1-2</span></a>). If <var class="Arg">str</var> does not contain such a sequence then <var class="Arg">str</var> itself is returned.</p>

<p><a id="X7D71CB837EE969D4" name="X7D71CB837EE969D4"></a></p>

<h5>6.1-8 RepeatedString</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RepeatedString</code>( <var class="Arg">c</var>, <var class="Arg">len</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RepeatedUTF8String</code>( <var class="Arg">c</var>, <var class="Arg">len</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Here <var class="Arg">c</var> must be either a character or a string and <var class="Arg">len</var> is a non-negative number. Then <code class="func">RepeatedString</code> returns a string of length <var class="Arg">len</var> consisting of copies of <var class="Arg">c</var>.</p>

<p>In the variant <code class="func">RepeatedUTF8String</code> the argument <var class="Arg">c</var> is considered as string in UTF-8 encoding, and it can also be specified as unicode string or character, see <code class="func">Unicode</code> (<a href="chap6.html#X8475671278948DDD"><span class="RefLink">6.2-1</span></a>). The result is a string in UTF-8 encoding which has visible width <var class="Arg">len</var> as explained in <code class="func">WidthUTF8String</code> (<a href="chap6.html#X801237207E06A876"><span class="RefLink">6.2-3</span></a>).</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RepeatedString('=',51);</span>
"==================================================="
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RepeatedString("*=",51);</span>
"*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*"
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">s := "bäh";;</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">enc := GAPInfo.TermEncoding;;</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">if enc &lt;&gt; "UTF-8" then s := Encode(Unicode(s, enc), "UTF-8"); fi;</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := RepeatedUTF8String(s, 8);;</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">u := Unicode(l, "UTF-8");;</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Print(Encode(u, enc), "\n");</span>
bähbähbä
</pre></div>

<p><a id="X7CEEA5B57D7BB38F" name="X7CEEA5B57D7BB38F"></a></p>

<h5>6.1-9 NumberDigits</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; NumberDigits</code>( <var class="Arg">str</var>, <var class="Arg">base</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: integer</p>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; DigitsNumber</code>( <var class="Arg">n</var>, <var class="Arg">base</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: string</p>

<p>The argument <var class="Arg">str</var> of <code class="func">NumberDigits</code> must be a string consisting only of an optional leading <code class="code">'-'</code> and characters in <code class="code">0123456789abcdefABCDEF</code>, describing an integer in base <var class="Arg">base</var> with <span class="SimpleMath">2 ≤ <var class="Arg">base</var> ≤ 16</span>. This function returns the corresponding integer.</p>

<p>The function <code class="func">DigitsNumber</code> does the reverse.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">NumberDigits("1A3F",16);</span>
6719
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">DigitsNumber(6719, 16);</span>
"1A3F"
</pre></div>

<p><a id="X79EF038284598D41" name="X79EF038284598D41"></a></p>

<h5>6.1-10 LabelInt</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; LabelInt</code>( <var class="Arg">n</var>, <var class="Arg">type</var>, <var class="Arg">pre</var>, <var class="Arg">post</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: string</p>

<p>The argument <var class="Arg">n</var> must be an integer in the range from 1 to 5000, while <var class="Arg">pre</var> and <var class="Arg">post</var> must be strings.</p>

<p>The argument <var class="Arg">type</var> can be one of <code class="code">"Decimal"</code>, <code class="code">"Roman"</code>, <code class="code">"roman"</code>, <code class="code">"Alpha"</code>, <code class="code">"alpha"</code>.</p>

<p>The function returns a string that starts with <var class="Arg">pre</var>, followed by a decimal, respectively roman number or alphanumerical number literal (capital, respectively small letters), followed by <var class="Arg">post</var>.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List([1,2,3,4,5,691], i-&gt; LabelInt(i,"Decimal","","."));</span>
[ "1.", "2.", "3.", "4.", "5.", "691." ]
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List([1,2,3,4,5,691], i-&gt; LabelInt(i,"alpha","(",")"));</span>
[ "(a)", "(b)", "(c)", "(d)", "(e)", "(zo)" ]
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List([1,2,3,4,5,691], i-&gt; LabelInt(i,"alpha","(",")"));</span>
[ "(a)", "(b)", "(c)", "(d)", "(e)", "(zo)" ]
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List([1,2,3,4,5,691], i-&gt; LabelInt(i,"Alpha","",".)"));</span>
[ "A.)", "B.)", "C.)", "D.)", "E.)", "ZO.)" ]
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List([1,2,3,4,5,691], i-&gt; LabelInt(i,"roman","","."));</span>
[ "i.", "ii.", "iii.", "iv.", "v.", "dcxci." ]
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List([1,2,3,4,5,691], i-&gt; LabelInt(i,"Roman","",""));</span>
[ "I", "II", "III", "IV", "V", "DCXCI" ]
</pre></div>

<p><a id="X7AF694D9839BF65C" name="X7AF694D9839BF65C"></a></p>

<h5>6.1-11 PositionMatchingDelimiter</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionMatchingDelimiter</code>( <var class="Arg">str</var>, <var class="Arg">delim</var>, <var class="Arg">pos</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: position as integer or <code class="keyw">fail</code></p>

<p>Here <var class="Arg">str</var> must be a string and <var class="Arg">delim</var> a string with two different characters. This function searches the smallest position <code class="code">r</code> of the character <code class="code"><var class="Arg">delim</var>[2]</code> in <var class="Arg">str</var> such that the number of occurrences of <code class="code"><var class="Arg">delim</var>[2]</code> in <var class="Arg">str</var> between positions <code class="code"><var class="Arg">pos</var>+1</code> and <code class="code">r</code> is by one greater than the corresponding number of occurrences of <code class="code"><var class="Arg">delim</var>[1]</code>.</p>

<p>If such an <code class="code">r</code> exists, it is returned. Otherwise <code class="keyw">fail</code> is returned.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionMatchingDelimiter("{}x{ab{c}d}", "{}", 0);</span>
fail
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionMatchingDelimiter("{}x{ab{c}d}", "{}", 1);</span>
2
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionMatchingDelimiter("{}x{ab{c}d}", "{}", 6);</span>
11
</pre></div>

<p><a id="X832556617F10AAA8" name="X832556617F10AAA8"></a></p>

<h5>6.1-12 WordsString</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; WordsString</code>( <var class="Arg">str</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: list of strings containing the words</p>

<p>This returns the list of words of a text stored in the string <var class="Arg">str</var>. All non-letters are considered as word boundaries and are removed.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">WordsString("one_two \n    three!?");</span>
[ "one", "two", "three" ]
</pre></div>

<p><a id="X83F2821783DA9826" name="X83F2821783DA9826"></a></p>

<h5>6.1-13 Base64String</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Base64String</code>( <var class="Arg">str</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; StringBase64</code>( <var class="Arg">bstr</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: a string</p>

<p>The first function translates arbitrary binary data given as a GAP string into a <em>base 64</em> encoded string. This encoded string contains only printable ASCII characters and is used in various data transfer protocols (<code class="code">MIME</code> encoded emails, weak password encryption, ...). We use the specification in <span class="URL"><a href="https://www.rfc-editor.org/rfc/rfc2045">RFC 2045</a></span>.</p>

<p>The second function has the reverse functionality. Here we also accept the characters <code class="code">-_</code> instead of <code class="code">+/</code> as last two characters. Whitespace is ignored.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">b := Base64String("This is a secret!");</span>
"VGhpcyBpcyBhIHNlY3JldCEA="
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">StringBase64(b);                       </span>
"This is a secret!"
</pre></div>

<p><a id="X8489C67D80399814" name="X8489C67D80399814"></a></p>

<h4>6.2 <span class="Heading">Unicode Strings</span></h4>

<p>The <strong class="pkg">GAPDoc</strong> package provides some tools to deal with unicode characters and strings. These can be used for recoding text strings between various encodings.</p>

<p><a id="X8475671278948DDD" name="X8475671278948DDD"></a></p>

<h5>6.2-1 <span class="Heading">Unicode Strings and Characters</span></h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Unicode</code>( <var class="Arg">list</var>[, <var class="Arg">encoding</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; UChar</code>( <var class="Arg">num</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsUnicodeString</code></td><td class="tdright">(&nbsp;filter&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsUnicodeCharacter</code></td><td class="tdright">(&nbsp;filter&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IntListUnicodeString</code>( <var class="Arg">ustr</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Unicode characters are described by their <em>codepoint</em>, an integer in the range from <span class="SimpleMath">0</span> to <span class="SimpleMath">2^21-1</span>. For details about unicode, see <span class="URL"><a href="https://www.unicode.org">https://www.unicode.org</a></span>.</p>

<p>The function <code class="func">UChar</code> wraps an integer <var class="Arg">num</var> into a <strong class="pkg">GAP</strong> object lying in the filter <code class="func">IsUnicodeCharacter</code>. Use <code class="code">Int</code> to get the codepoint back. The argument <var class="Arg">num</var> can also be a <strong class="pkg">GAP</strong> character which is then translated to an integer via <code class="func">IntChar</code> (<a href="../../../doc/ref/chap27.html#X826D95D680F87D23"><span class="RefLink">Reference: IntChar</span></a>).</p>

<p><code class="func">Unicode</code> produces a <strong class="pkg">GAP</strong> object in the filter <code class="func">IsUnicodeString</code>. This is a wrapped list of integers for the unicode characters in the string. The function <code class="func">IntListUnicodeString</code> gives access to this list of integers. Basic list functionality is available for <code class="func">IsUnicodeString</code> elements. The entries are in <code class="func">IsUnicodeCharacter</code>. The argument <var class="Arg">list</var> for <code class="func">Unicode</code> is either a list of integers or a <strong class="pkg">GAP</strong> string. In the latter case an <var class="Arg">encoding</var> can be specified as string, its default is <code class="code">"UTF-8"</code>.</p>

<p>Currently supported encodings can be found in <code class="code">UNICODE_RECODE.NormalizedEncodings</code> (ASCII, ISO-8859-X, UTF-8 and aliases). The encoding <code class="code">"XML"</code> means an ASCII encoding in which non-ASCII characters are specified by XML character entities. The encoding <code class="code">"URL"</code> is for URL-encoded (also called percent-encoded strings, as specified in RFC 3986 (<span class="URL"><a href="https://www.ietf.org/rfc/rfc3986.txt">see here</a></span>). The listed encodings <code class="code">"LaTeX"</code> and aliases cannot be used with <code class="func">Unicode</code>. See the operation <code class="func">Encode</code> (<a href="chap6.html#X818A31567EB30A39"><span class="RefLink">6.2-2</span></a>) for mapping a unicode string to a <strong class="pkg">GAP</strong> string.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ustr := Unicode("a and \366", "latin1");</span>
Unicode("a and ö")
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ustr = Unicode("a and &amp;#246;", "XML");  </span>
true
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IntListUnicodeString(ustr);</span>
[ 97, 32, 97, 110, 100, 32, 246 ]
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ustr[7];</span>
'ö'
</pre></div>

<p><a id="X818A31567EB30A39" name="X818A31567EB30A39"></a></p>

<h5>6.2-2 Encode</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Encode</code>( <var class="Arg">ustr</var>[, <var class="Arg">encoding</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
<p>Returns: a <strong class="pkg">GAP</strong> string</p>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SimplifiedUnicodeString</code>( <var class="Arg">ustr</var>[, <var class="Arg">encoding</var>][, <var class="Arg">"single"</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: a unicode string</p>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; LowercaseUnicodeString</code>( <var class="Arg">ustr</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: a unicode string</p>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; UppercaseUnicodeString</code>( <var class="Arg">ustr</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: a unicode string</p>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; LaTeXUnicodeTable</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SimplifiedUnicodeTable</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; LowercaseUnicodeTable</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
<p>The operation <code class="func">Encode</code> translates a unicode string <var class="Arg">ustr</var> into a <strong class="pkg">GAP</strong> string in some specified <var class="Arg">encoding</var>. The default encoding is <code class="code">"UTF-8"</code>.</p>

<p>Supported encodings can be found in <code class="code">UNICODE_RECODE.NormalizedEncodings</code>. Except for some cases mentioned below characters which are not available in the target encoding are substituted by '?' characters.</p>

<p>If the <var class="Arg">encoding</var> is <code class="code">"URL"</code> (see <code class="func">Unicode</code> (<a href="chap6.html#X8475671278948DDD"><span class="RefLink">6.2-1</span></a>)) then an optional argument <var class="Arg">encreserved</var> can be given, it must be a list of reserved characters which should be percent encoded; the default is to encode only the <code class="code">%</code> character.</p>

<p>The encoding <code class="code">"LaTeX"</code> substitutes non-ASCII characters and LaTeX special characters by LaTeX code as given in an ordered list <code class="code">LaTeXUnicodeTable</code> of pairs [codepoint, string]. If you have a unicode character for which no substitution is contained in that list, you will get a warning and the translation is <code class="code">Unicode(nr)</code>. In this case find a substitution and add a corresponding [codepoint, string] pair to <code class="code">LaTeXUnicodeTable</code> using <code class="func">AddSet</code> (<a href="../../../doc/ref/chap21.html#X832C23CC7FCD8892"><span class="RefLink">Reference: AddSet</span></a>). Also, please, tell the <strong class="pkg">GAPDoc</strong> authors about your addition, such that we can extend the list <code class="code">LaTeXUnicodeTable</code>. (Most of the initial entries were generated from lists in the TeX projects encTeX and <code class="code">ucs</code>.) There are some variants of this encoding:</p>

<p><code class="code">"LaTeXleavemarkup"</code> does the same translations for non-ASCII characters but leaves the LaTeX special characters (e.g., any LaTeX commands) as they are.</p>

<p><code class="code">"LaTeXUTF8"</code> does not give a warning about unicode characters without explicit translation, instead it translates the character to its <code class="code">UTF-8</code> encoding. Make sure to setup your LaTeX document such that all these characters are understood.</p>

<p><code class="code">"LaTeXUTF8leavemarkup"</code> is a combination of the last two variants.</p>

<p>Note that the <code class="code">"LaTeX"</code> encoding can only be used with <code class="func">Encode</code> but not for the opposite translation with <code class="func">Unicode</code> (<a href="chap6.html#X8475671278948DDD"><span class="RefLink">6.2-1</span></a>) (which would need far too complicated heuristics).</p>

<p>The function <code class="func">SimplifiedUnicodeString</code> can be used to substitute many non-ASCII characters by related ASCII characters or strings (e.g., by a corresponding character without accents). The argument <var class="Arg">ustr</var> and the result are unicode strings, if <var class="Arg">encoding</var> is <code class="code">"ASCII"</code> then all non-ASCII characters are translated, otherwise only the non-latin1 characters. If the string <code class="code">"single"</code> in an argument then only substitutions are considered which don't make the result string longer. The translations are stored in a sorted list <code class="code">SimplifiedUnicodeTable</code>. Its entries are of the form <code class="code">[codepoint, trans1, trans2, ...]</code>. Here <code class="code">trans1</code> and so on is either an integer for the codepoint of a substitution character or it is a list of codepoint integers. If you are missing characters in this list and know a sensible ASCII approximation, then add an entry (with <code class="func">AddSet</code> (<a href="../../../doc/ref/chap21.html#X832C23CC7FCD8892"><span class="RefLink">Reference: AddSet</span></a>)) and tell the <strong class="pkg">GAPDoc</strong> authors about it. (The initial content of <code class="code">SimplifiedUnicodeTable</code> was mainly generated from the <q><code class="code">transtab</code></q> tables by Markus Kuhn.)</p>

<p>The function <code class="func">LowercaseUnicodeString</code> gets and returns a unicode string and translates each uppercase character to its corresponding lowercase version. This function uses a list <code class="code">LowercaseUnicodeTable</code> of pairs of codepoint integers. This list was generated using the file <code class="file">UnicodeData.txt</code> from the unicode definition (field 14 in each row).</p>

<p>The function <code class="func">UppercaseUnicodeString</code> does the similar translation to uppercase characters.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ustr := Unicode("a and &amp;#246;", "XML");</span>
Unicode("a and ö")
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SimplifiedUnicodeString(ustr, "ASCII");</span>
Unicode("a and oe")
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SimplifiedUnicodeString(ustr, "ASCII", "single");</span>
Unicode("a and o")
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ustr2 := UppercaseUnicodeString(ustr);;</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Print(Encode(ustr2, GAPInfo.TermEncoding), "\n");</span>
A AND Ö
</pre></div>

<p><a id="X801237207E06A876" name="X801237207E06A876"></a></p>

<h5>6.2-3 <span class="Heading">Lengths of UTF-8 strings</span></h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; WidthUTF8String</code>( <var class="Arg">str</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; NrCharsUTF8String</code>( <var class="Arg">str</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: an integer</p>

<p>Let <var class="Arg">str</var> be a <strong class="pkg">GAP</strong> string with text in UTF-8 encoding. There are three <q>lengths</q> of such a string which must be distinguished. The operation <code class="func">Length</code> (<a href="../../../doc/ref/chap21.html#X780769238600AFD1"><span class="RefLink">Reference: Length</span></a>) returns the number of bytes and so the memory occupied by <var class="Arg">str</var>. The function <code class="func">NrCharsUTF8String</code> returns the number of unicode characters in <var class="Arg">str</var>, that is the length of <code class="code">Unicode(<var class="Arg">str</var>)</code>.</p>

<p>In many applications the function <code class="func">WidthUTF8String</code> is more interesting, it returns the number of columns needed by the string if printed to a terminal. This takes into account that some unicode characters are combining characters and that there are wide characters which need two columns (e.g., for Chinese or Japanese). (To be precise: This implementation assumes that there are no control characters in <var class="Arg">str</var> and uses the character width returned by the <code class="code">wcwidth</code> function in the GNU C-library called with UTF-8 locale.)</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># A, German umlaut u, B, zero width space, C, newline</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">str := Encode( Unicode( "A&amp;#xFC;B&amp;#x200B;C\n", "XML" ) );;</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Print(str);</span>
AüB​C
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># umlaut u needs two bytes and the zero width space three</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Length(str);</span>
9
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">NrCharsUTF8String(str);</span>
6
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># zero width space and newline don't contribute to width</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">WidthUTF8String(str);</span>
4
</pre></div>

<p><a id="X7E2974CD84977819" name="X7E2974CD84977819"></a></p>

<h5>6.2-4 InitialSubstringUTF8String</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; InitialSubstringUTF8String</code>( <var class="Arg">str</var>, <var class="Arg">maxwidth</var>[, <var class="Arg">suf</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>Returns: UTF-8 encoded string</p>

<p>The arguments <var class="Arg">str</var> and <var class="Arg">suf</var> each must be a <strong class="pkg">GAP</strong> string with text in UTF-8 encoding or a unicode string. The argument <var class="Arg">suf</var> is optional and its default value is the empty string. If the visible width of <var class="Arg">str</var> is at most <var class="Arg">maxwidth</var> then <var class="Arg">str</var> is returned as UTF-8 encoded <strong class="pkg">GAP</strong> string. Otherwise, <var class="Arg">suf</var> is appended to the maximal initial substring of <var class="Arg">str</var> such that the total visible width of the result is at most <var class="Arg">maxwidth</var>.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># A, German umlaut u, B, zero width space, C, newline</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">str := Encode( Unicode( "A&amp;#xFC;B&amp;#x200B;C\n", "XML" ) );;</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ini := InitialSubstringUTF8String(str, 3);;</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">WidthUTF8String(ini);</span>
3
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IntListUnicodeString(Unicode(ini));</span>
[ 65, 252, 66, 8203 ]
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := Unicode([ 23380, 22827, 23376 ] );; # three chars of width 2</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">s := InitialSubstringUTF8String(l, 4, "*");;</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">WidthUTF8String(s);</span>
3
</pre></div>

<p><a id="X860C83047DC4F1BC" name="X860C83047DC4F1BC"></a></p>

<h4>6.3 <span class="Heading">Print Utilities</span></h4>

<p>The following printing utilities turned out to be useful for interactive work with texts in <strong class="pkg">GAP</strong>. But they are more general and so we document them here.</p>

<p><a id="X8603B90C7C3F0AB1" name="X8603B90C7C3F0AB1"></a></p>

<h5>6.3-1 PrintTo1</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PrintTo1</code>( <var class="Arg">filename</var>, <var class="Arg">fun</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AppendTo1</code>( <var class="Arg">filename</var>, <var class="Arg">fun</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>The argument <var class="Arg">fun</var> must be a function without arguments. Everything which is printed by a call <var class="Arg">fun()</var> is printed into the file <var class="Arg">filename</var>. As with <code class="func">PrintTo</code> (<a href="../../../doc/ref/chap9.html#X86956C577FFEE1F9"><span class="RefLink">Reference: PrintTo</span></a>) and <code class="func">AppendTo</code> (<a href="../../../doc/ref/chap9.html#X86956C577FFEE1F9"><span class="RefLink">Reference: AppendTo</span></a>) this overwrites or appends to, respectively, a previous content of <var class="Arg">filename</var>.</p>

<p>These functions can be particularly efficient when many small pieces of text shall be written to a file, because no multiple reopening of the file is necessary.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">f := function() local i; </span>
<span class="GAPprompt">&gt;</span> <span class="GAPinput">  for i in [1..100000] do Print(i, "\n"); od; end;; </span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrintTo1("nonsense", f); # now check the local file `nonsense'</span>
</pre></div>

<p><a id="X829B720C86E57E8B" name="X829B720C86E57E8B"></a></p>

<h5>6.3-2 StringPrint</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; StringPrint</code>( <var class="Arg">obj1</var>[, <var class="Arg">obj2</var>[, <var class="Arg">...</var>]] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; StringView</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; StringDisplay</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>These functions return a string containing the output of a <code class="code">Print</code>, <code class="code">ViewObj</code> or <code class="code">Display</code> call, respectively, with the same arguments.</p>

<p>This should be considered as a (temporary?) hack. It would be better to have <code class="func">String</code> (<a href="../../../doc/ref/chap27.html#X81FB5BE27903EC32"><span class="RefLink">Reference: String</span></a>) methods for all <strong class="pkg">GAP</strong> objects and to have a generic <code class="func">Print</code> (<a href="../../../doc/ref/chap6.html#X7AFA64D97A1F39A3"><span class="RefLink">Reference: Print</span></a>)-function which just interprets these strings.</p>

<p><a id="X812A8326844BC910" name="X812A8326844BC910"></a></p>

<h5>6.3-3 PrintFormattedString</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PrintFormattedString</code>( <var class="Arg">str</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>This function prints a string <var class="Arg">str</var>. The difference to <code class="code">Print(str);</code> is that no additional line breaks are introduced by <strong class="pkg">GAP</strong>'s standard printing mechanism. This can be used to print lines which are longer than the current screen width. In particular one can print text which contains escape sequences like those explained in <code class="func">TextAttr</code> (<a href="chap6.html#X785F61E77899580E"><span class="RefLink">6.1-2</span></a>), where lines may have more characters than <em>visible characters</em>.</p>

<p><a id="X7BB6731F7E3AAA98" name="X7BB6731F7E3AAA98"></a></p>

<h5>6.3-4 Page</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Page</code>( <var class="Arg">...</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PageDisplay</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>These functions are similar to <code class="func">Print</code> (<a href="../../../doc/ref/chap6.html#X7AFA64D97A1F39A3"><span class="RefLink">Reference: Print</span></a>) and <code class="func">Display</code> (<a href="../../../doc/ref/chap6.html#X83A5C59278E13248"><span class="RefLink">Reference: Display</span></a>), respectively. The difference is that the output is not sent directly to the screen, but is piped into the current pager; see <code class="func">Pager</code> (<a href="../../../doc/ref/chap2.html#X7ED03E41792C3840"><span class="RefLink">Reference: Pager</span></a>).</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Page([1..1421]+0);</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PageDisplay(CharacterTable("Symmetric", 14));</span>
</pre></div>

<p><a id="X7E14D32181FBC3C3" name="X7E14D32181FBC3C3"></a></p>

<h5>6.3-5 StringFile</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; StringFile</code>( <var class="Arg">filename</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; FileString</code>( <var class="Arg">filename</var>, <var class="Arg">str</var>[, <var class="Arg">append</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>The function <code class="func">StringFile</code> returns the content of file <var class="Arg">filename</var> as a string. This works efficiently with arbitrary (binary or text) files. If something went wrong, this function returns <code class="keyw">fail</code>.</p>

<p>Conversely the function <code class="func">FileString</code> writes the content of a string <var class="Arg">str</var> into the file <var class="Arg">filename</var>. If the optional third argument <var class="Arg">append</var> is given and equals <code class="keyw">true</code> then the content of <var class="Arg">str</var> is appended to the file. Otherwise previous content of the file is deleted. This function returns the number of bytes written or <code class="keyw">fail</code> if something went wrong.</p>

<p>Both functions are quite efficient, even with large files.</p>


<div class="chlinkprevnextbot">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp;  <a href="chap0.html#contents">[Contents]</a>&nbsp;  &nbsp;<a href="chap5.html">[Previous Chapter]</a>&nbsp;  &nbsp;<a href="chap7.html">[Next Chapter]</a>&nbsp;  </div>


<div class="chlinkbot"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a>  <a href="chap1.html">1</a>  <a href="chap2.html">2</a>  <a href="chap3.html">3</a>  <a href="chap4.html">4</a>  <a href="chap5.html">5</a>  <a href="chap6.html">6</a>  <a href="chap7.html">7</a>  <a href="chapA.html">A</a>  <a href="chapB.html">B</a>  <a href="chapC.html">C</a>  <a href="chapBib.html">Bib</a>  <a href="chapInd.html">Ind</a>  </div>

<hr />
<p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
</body>
</html>