forked from modelica/ModelicaStandardLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strings.mo
1433 lines (1383 loc) · 57.1 KB
/
Strings.mo
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
within Modelica.Utilities;
package Strings "Operations on strings"
extends Modelica.Icons.FunctionsPackage;
pure function length "Return length of string"
extends Modelica.Icons.Function;
input String string;
output Integer result "Number of characters of string";
external "C" result = ModelicaStrings_length(string) annotation(IncludeDirectory="modelica://Modelica/Resources/C-Sources", Include="#include \"ModelicaStrings.h\"", Library="ModelicaExternalC");
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Strings.<strong>length</strong>(string);
</pre></blockquote>
<h4>Description</h4>
<p>
Returns the number of characters of \"string\".
</p>
</html>"));
end length;
pure function substring "Return a substring defined by start and end index"
extends Modelica.Icons.Function;
input String string "String from which a substring is inquired";
input Integer startIndex(min=1)
"Character position of substring begin (index=1 is first character in string)";
input Integer endIndex "Character position of substring end";
output String result
"String containing substring string[startIndex:endIndex]";
external "C" result = ModelicaStrings_substring(string,startIndex,endIndex) annotation(IncludeDirectory="modelica://Modelica/Resources/C-Sources", Include="#include \"ModelicaStrings.h\"", Library="ModelicaExternalC");
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
string2 = Strings.<strong>substring</strong>(string, startIndex, endIndex);
</pre></blockquote>
<h4>Description</h4>
<p>
This function returns
the substring from position startIndex
up to and including position endIndex of \"string\" .
The substring computation has the following properties:
</p>
<ul>
<li>If the <code>startIndex</code> is non-positive, it is set to 1 and a warning is raised.</li>
<li>If the <code>startIndex</code> exceeds the string length, the returned substring is empty.</li>
<li>If the <code>endIndex</code> is negative, it is set to the <code>startIndex</code> and a warning is raised. The returned substring is the single character at position <code>startIndex</code> of <code>string</code>.</li>
<li>If the <code>endIndex</code> is non-negative and less than the <code>startIndex</code>, the returned substring is empty.</li>
<li>If the <code>endIndex</code> exceeds the string length, it is set to the string length.</li>
<li>It is only intended for ASCII encoded strings. For UTF-8 encoded strings the index is seen as bytes, and some index values may break the string inside glyphs or even inside code points.</li>
</ul>
<h4>Example</h4>
<blockquote><pre>
string1 := \"This is line 111\";
string2 := Strings.substring(string1,9,12); // string2 = \"line\"
string3 := Strings.substring(string1,9,0); // string3 = \"\"
</pre></blockquote>
</html>"));
end substring;
function repeat "Repeat a string n times"
extends Modelica.Icons.Function;
input Integer n(min=0) = 1 "Number of occurrences";
input String string=" " "String that is repeated";
output String repeatedString "String containing n concatenated strings";
algorithm
repeatedString :="";
for i in 1:n loop
repeatedString := repeatedString + string;
end for;
annotation (
Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
string2 = Strings.<strong>repeat</strong>(n);
string2 = Strings.<strong>repeat</strong>(n, string=\" \");
</pre></blockquote>
<h4>Description</h4>
<p>
The first form returns a string consisting of n blanks.
</p>
<p>
The second form returns a string consisting of n substrings
defined by the optional argument \"string\".
</p>
</html>"));
end repeat;
pure function compare "Compare two strings lexicographically"
extends Modelica.Icons.Function;
input String string1;
input String string2;
input Boolean caseSensitive=true "= false, if case of letters is ignored";
output Modelica.Utilities.Types.Compare result "Result of comparison";
external "C" result = ModelicaStrings_compare(string1, string2, caseSensitive) annotation(IncludeDirectory="modelica://Modelica/Resources/C-Sources", Include="#include \"ModelicaStrings.h\"", Library="ModelicaExternalC");
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
result = Strings.<strong>compare</strong>(string1, string2);
result = Strings.<strong>compare</strong>(string1, string2, caseSensitive=true);
</pre></blockquote>
<h4>Description</h4>
<p>
Compares two strings. If the optional argument caseSensitive=false,
upper case letters are treated as if they would be lower case letters.
The result of the comparison is returned as:
</p>
<blockquote><pre>
result = Modelica.Utilities.Types.Compare.Less // string1 < string2
= Modelica.Utilities.Types.Compare.Equal // string1 = string2
= Modelica.Utilities.Types.Compare.Greater // string1 > string2
</pre></blockquote>
<p>
Comparison is with regards to lexicographical order,
e.g., \"a\" < \"b\";
</p>
<p>
It is intended for ASCII, the case-insensitive comparison is not guaranteed to work for UTF-8.
</p>
</html>"));
end compare;
function isEqual "Determine whether two strings are identical"
extends Modelica.Icons.Function;
input String string1;
input String string2;
input Boolean caseSensitive=true
"= false, if lower and upper case are ignored for the comparison";
output Boolean identical "True, if string1 is identical to string2";
algorithm
identical :=compare(string1, string2, caseSensitive) == Types.Compare.Equal;
annotation (
Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Strings.<strong>isEqual</strong>(string1, string2);
Strings.<strong>isEqual</strong>(string1, string2, caseSensitive=true);
</pre></blockquote>
<h4>Description</h4>
<p>
Compare whether two strings are identical,
optionally ignoring case.
</p>
<p>
It is intended for ASCII, the case-insensitive comparison is not guaranteed to work for UTF-8.
</p>
</html>"));
end isEqual;
function isEmpty
"Return true if a string is empty (has only white space characters)"
extends Modelica.Icons.Function;
input String string;
output Boolean result "True, if string is empty";
protected
Integer nextIndex;
Integer len;
algorithm
nextIndex := Strings.Advanced.skipWhiteSpace(string);
len := Strings.length(string);
if len < 1 or nextIndex > len then
result := true;
else
result := false;
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Strings.<strong>isEmpty</strong>(string);
</pre></blockquote>
<h4>Description</h4>
<p>
Returns true if the string has no characters or if the string consists
only of white space characters. Otherwise, false is returned.
It is intended for ASCII, white space in UTF-8 is more complicated.
</p>
<h4>Example</h4>
<blockquote><pre>
isEmpty(\"\"); // returns true
isEmpty(\" \"); // returns true
isEmpty(\" abc\"); // returns false
isEmpty(\"a\"); // returns false
</pre></blockquote>
</html>"));
end isEmpty;
function count "Count the number of non-overlapping occurrences of a string"
extends Modelica.Icons.Function;
input String string "String that is analyzed";
input String searchString "String that is searched for in string";
input Integer startIndex(min=1)=1 "Start search at index startIndex";
input Boolean caseSensitive=true
"= false, if lower and upper case are ignored for count";
output Integer result "Number of occurrences of 'searchString' in 'string'";
protected
Integer lenSearchString = length(searchString);
Integer i = startIndex;
algorithm
result := 0;
while i <> 0 loop
i := find(string, searchString, i, caseSensitive);
if i > 0 then
result := result + 1;
i := i + lenSearchString;
end if;
end while;
annotation (
Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Strings.<strong>count</strong>(string, searchString)
Strings.<strong>count</strong>(string, searchString, startIndex=1,
caseSensitive=true)
</pre></blockquote>
<h4>Description</h4>
<p>
Returns the number of non-overlapping occurrences of string \"searchString\"
in \"string\". The search is started at index \"startIndex\" (default = 1).
If the optional argument \"caseSensitive\" is false,
for the counting it does not matter whether a letter is upper
or lower case.
</p>
<p>
It is intended for ASCII, the case-insensitive count is not guaranteed to work for UTF-8.
</p>
</html>"));
end count;
function find "Find first occurrence of a string within another string"
extends Modelica.Icons.Function;
input String string "String that is analyzed";
input String searchString "String that is searched for in string";
input Integer startIndex(min=1)=1 "Start search at index startIndex";
input Boolean caseSensitive=true
"= false, if lower and upper case are ignored for the search";
output Integer index
"Index of the beginning of the first occurrence of 'searchString' within 'string', or zero if not present";
protected
Integer lengthSearchString = length(searchString);
Integer len = lengthSearchString-1;
Integer i = startIndex;
Integer i_max = length(string) - lengthSearchString + 1;
algorithm
index := 0;
while i <= i_max loop
if isEqual(substring(string,i,i+len),
searchString, caseSensitive) then
index := i;
i := i_max + 1;
else
i := i+1;
end if;
end while;
annotation (
Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
index = Strings.<strong>find</strong>(string, searchString);
index = Strings.<strong>find</strong>(string, searchString, startIndex=1,
caseSensitive=true);
</pre></blockquote>
<h4>Description</h4>
<p>
Finds first occurrence of \"searchString\" within \"string\"
and return the corresponding index.
Start search at index \"startIndex\" (default = 1).
If the optional argument \"caseSensitive\" is false, lower
and upper case are ignored for the search.
If \"searchString\" is not found, a value of \"0\" is returned.
</p>
<p>
It is intended for ASCII, the case-insensitive count is not guaranteed to work for UTF-8.
</p>
</html>"));
end find;
function findLast "Find last occurrence of a string within another string"
extends Modelica.Icons.Function;
input String string "String that is analyzed";
input String searchString "String that is searched for in string";
input Integer startIndex(min=0)=0
"Start search at index startIndex. If startIndex = 0, start at length(string)";
input Boolean caseSensitive=true
"= false, if lower and upper case are ignored for the search";
output Integer index
"Index of the beginning of the last occurrence of 'searchString' within 'string', or zero if not present";
protected
Integer lenString = length(string);
Integer lenSearchString = length(searchString);
Integer iMax=lenString - lenSearchString + 1;
Integer i;
algorithm
i := if startIndex == 0 or startIndex > iMax then iMax else startIndex;
index := 0;
while i >= 1 loop
if isEqual(substring(string,i,i+lenSearchString-1),
searchString, caseSensitive) then
index := i;
i := 0;
else
i := i-1;
end if;
end while;
annotation (
Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
index = Strings.<strong>findLast</strong>(string, searchString);
index = Strings.<strong>findLast</strong>(string, searchString,
startIndex=length(string), caseSensitive=true,
</pre></blockquote>
<h4>Description</h4>
<p>
Finds first occurrence of \"searchString\" within \"string\"
when searching from the last character of \"string\"
backwards, and return the corresponding index.
Start search at index \"startIndex\" (default = 0;
if startIndex = 0, search starts at length(string)).
If the optional argument \"caseSensitive\" is false, lower
and upper case are ignored for the search.
If \"searchString\" is not found, a value of \"0\" is returned.
</p>
<p>
It is intended for ASCII, the case-insensitive count is not guaranteed to work for UTF-8.
</p>
</html>"));
end findLast;
function replace
"Replace non-overlapping occurrences of a string from left to right"
extends Modelica.Icons.Function;
input String string "String to be modified";
input String searchString
"Replace non-overlapping occurrences of 'searchString' in 'string' with 'replaceString'";
input String replaceString
"String that replaces 'searchString' in 'string'";
input Integer startIndex=1 "Start search at index startIndex";
input Boolean replaceAll=true
"= false, if only the first occurrence is replaced, otherwise all occurrences";
input Boolean caseSensitive=true
"= false, if lower and upper case are ignored when searching for searchString";
output String result "Resultant string of replacement operation";
protected
Integer lenString = length(string);
Integer lenSearchString = length(searchString);
Integer i = startIndex;
Integer i_found;
algorithm
result := if startIndex == 1 then "" else substring(string,1,startIndex-1);
while i > 0 loop
i_found := find(string, searchString, i, caseSensitive);
if i_found > 0 then
result := if i_found == 1 then
replaceString else
result + (if i_found-1<i then "" else substring(string, i, i_found-1)) + replaceString;
i := i_found + lenSearchString;
if i > lenString then
i := 0;
elseif not replaceAll then
result := result + substring(string, i, lenString);
i := 0;
end if;
elseif lenString<i then
i := 0;
else
result := result + substring(string, i, lenString);
i := 0;
end if;
end while;
annotation (
Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Strings.<strong>replace</strong>(string, searchString, replaceString);
Strings.<strong>replace</strong>(string, searchString, replaceString,
startIndex=1, replaceAll=true, caseSensitive=true);
</pre></blockquote>
<h4>Description</h4>
<p>
Search in \"string\" for \"searchString\" and replace the found
substring by \"replaceString\".
</p>
<ul>
<li> The search starts at the first character of \"string\",
or at character position \"startIndex\",
if this optional argument is provided.</li>
<li> If the optional argument \"replaceAll\" is <strong>true</strong> (default),
all occurrences of \"searchString\" are replaced.
If the argument is <strong>false</strong>, only the first occurrence
is replaced.</li>
<li> The search for \"searchString\" distinguishes upper and lower
case letters. If the optional argument \"caseSensitive\" is
<strong>false</strong>,
the search ignores whether letters are upper
or lower case.</li>
<li> It is intended for ASCII. The case-insensitive search is not guaranteed to work for UTF-8.</li>
</ul>
<p>
The function returns the \"string\" with the
performed replacements.
</p>
</html>"));
end replace;
function sort "Sort vector of strings in alphabetic order"
extends Modelica.Icons.Function;
input String stringVector1[:] "Vector of strings";
input Boolean caseSensitive=true
"= false, if lower and upper case are ignored when comparing elements of stringVector1";
output String stringVector2[size(stringVector1,1)]
"stringVector1 sorted in alphabetical order";
/* shellsort algorithm; should be improved later */
protected
Integer gap;
Integer i;
Integer j;
String tempString;
Integer nStringVector1 = size(stringVector1,1);
Boolean swap;
algorithm
stringVector2 := stringVector1;
gap := div(nStringVector1,2);
while gap > 0 loop
i := gap;
while i < nStringVector1 loop
j := i-gap;
if j >= 0 then
swap := compare(stringVector2[j+1], stringVector2[j+gap+1], caseSensitive)
== Modelica.Utilities.Types.Compare.Greater;
else
swap := false;
end if;
while swap loop
tempString := stringVector2[j+1];
stringVector2[j+1] := stringVector2[j+gap+1];
stringVector2[j+gap+1] := tempString;
j := j - gap;
if j >= 0 then
swap := compare(stringVector2[j+1], stringVector2[j+gap+1], caseSensitive)
== Modelica.Utilities.Types.Compare.Greater;
else
swap := false;
end if;
end while;
i := i + 1;
end while;
gap := div(gap,2);
end while;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
stringVector2 = Streams.<strong>sort</strong>(stringVector1);
stringVector2 = Streams.<strong>sort</strong>(stringVector1, caseSensitive=true);
</pre></blockquote>
<h4>Description</h4>
<p>
Function <strong>sort</strong>(..) sorts a string vector stringVector1
in lexicographical order and returns the result in stringVector2.
If the optional argument \"caseSensitive\" is <strong>false</strong>, lower
and upper case letters are not distinguished.
</p>
<h4>Example</h4>
<blockquote><pre>
s1 = {\"force\", \"angle\", \"pressure\"};
s2 = Strings.sort(s1);
-> s2 = {\"angle\", \"force\", \"pressure\"};
</pre></blockquote>
<p>It is intended for ASCII, the case-insensitive sort is not guaranteed to work for UTF-8.</p>
</html>"));
end sort;
pure function hashString "Create a hash value of a string"
extends Modelica.Icons.Function;
input String string "The string to create a hash from";
output Integer hash "The hash value of string";
external "C" hash= ModelicaStrings_hashString(string)
annotation(IncludeDirectory="modelica://Modelica/Resources/C-Sources", Include="#include \"ModelicaStrings.h\"", Library="ModelicaExternalC");
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
hash = Strings.<strong>hashString</strong>(string);
</pre></blockquote>
<h4>Description</h4>
<p>
Returns an Integer hash value of the provided string
(the hash can be any Integer, including zero or a negative number).
</p>
<h4>Example</h4>
<blockquote><pre>
hashString(\"this is a test\") // = 1827717433
hashString(\"Controller.noise1\") // = -1025762750
</pre></blockquote>
</html>", revisions="<html>
<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\">
<tr><th>Date</th> <th align=\"left\">Description</th></tr>
<tr><td> June 22, 2015 </td>
<td>
<table border=\"0\">
<tr><td>
<img src=\"modelica://Modelica/Resources/Images/Logos/dlr_logo.png\" alt=\"DLR logo\">
</td><td valign=\"bottom\">
Initial version implemented by
A. Klöckner, F. v.d. Linden, D. Zimmer, M. Otter.<br>
<a href=\"https://www.dlr.de/sr/en\">DLR Institute of System Dynamics and Control</a>
</td></tr></table>
</td></tr>
</table>
</html>"));
end hashString;
function scanToken "Scan for the next token and return it"
extends Modelica.Icons.Function;
input String string "String to be scanned";
input Integer startIndex(min=1) = 1
"Start scanning of string at character startIndex";
input Boolean unsigned=false
"= true, if Real and Integer tokens shall not start with a sign";
output Types.TokenValue token "Scanned token";
output Integer nextIndex
"Index of character after the found token; = 0, if NoToken";
protected
Integer startTokenIndex;
algorithm
// Initialize token
token.real :=0.0;
token.integer :=0;
token.boolean :=false;
token.string :="";
// skip white space and line comments
startTokenIndex := Advanced.skipLineComments(string, startIndex);
if startTokenIndex > length(string) then
token.tokenType := Modelica.Utilities.Types.TokenType.NoToken;
nextIndex := startTokenIndex;
else
// scan Integer number
(nextIndex, token.integer) := Advanced.scanInteger(string, startTokenIndex, unsigned);
token.tokenType := Types.TokenType.IntegerToken;
// scan Real number
if nextIndex == startTokenIndex then
(nextIndex, token.real) :=Advanced.scanReal(string, startTokenIndex, unsigned);
token.tokenType := Types.TokenType.RealToken;
end if;
// scan String
if nextIndex == startTokenIndex then
(nextIndex,token.string) := Advanced.scanString(string, startTokenIndex);
token.tokenType:= Types.TokenType.StringToken;
end if;
// scan Identifier or Boolean
if nextIndex == startTokenIndex then
(nextIndex,token.string) := Advanced.scanIdentifier(string, startTokenIndex);
if nextIndex > startTokenIndex then
if token.string == "false" then
token.string := "";
token.boolean :=false;
token.tokenType := Types.TokenType.BooleanToken;
elseif token.string == "true" then
token.string := "";
token.boolean := true;
token.tokenType := Types.TokenType.BooleanToken;
else
token.tokenType := Types.TokenType.IdentifierToken;
end if;
end if;
end if;
// scan Delimiter
if nextIndex == startTokenIndex then
token.string :=substring(string, startTokenIndex, startTokenIndex);
token.tokenType := Types.TokenType.DelimiterToken;
nextIndex := startTokenIndex + 1;
end if;
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
(token, nextIndex) = Strings.<strong>scanToken</strong>(string, startIndex, unsigned=false);
</pre></blockquote>
<h4>Description</h4>
<p>
Function <strong>scanToken</strong> scans the string starting at index
\"startIndex\" and returns the next token, as well as the
index directly after the token. The returned token is a record
that holds the type of the token and the value of the token:
</p>
<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\">
<tr><td>token.tokenType</td>
<td>Type of the token, see below</td></tr>
<tr><td>token.real</td>
<td>Real value if tokenType == TokenType.RealToken</td></tr>
<tr><td>token.integer</td>
<td>Integer value if tokenType == TokenType.IntegerToken</td></tr>
<tr><td>token.boolean</td>
<td>Boolean value if tokenType == TokenType.BooleanToken</td></tr>
<tr><td>token.string</td>
<td>String value if tokenType == TokenType.StringToken/IdentifierToken/DelimiterToken</td></tr>
</table>
<p>
Variable token.tokenType is an enumeration (emulated as a package
with constants) that can have the following values:
</p>
<blockquote><pre>
import T = Modelica.Utilities.Types.TokenType;
</pre></blockquote>
<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\">
<tr><td>T.RealToken</td>
<td>Modelica Real literal (e.g., 1.23e-4)</td></tr>
<tr><td>T.IntegerToken</td>
<td>Modelica Integer literal (e.g., 123)</td></tr>
<tr><td>T.BooleanToken</td>
<td>Modelica Boolean literal (e.g., false)</td></tr>
<tr><td>T.StringToken</td>
<td>Modelica String literal (e.g., \"string 123\")</td></tr>
<tr><td>T.IdentifierToken</td>
<td>Modelica identifier (e.g., \"force_a\")</td></tr>
<tr><td>T.DelimiterToken</td>
<td>any character without white space that does not appear<br>
as first character in the tokens above (e.g., \"&\")</td></tr>
<tr><td>T.NoToken</td>
<td>White space, line comments and no other token<br>
until the end of the string</td></tr>
</table>
<p>
Modelica line comments (\"// ... end-of-line/end-of-string\")
as well as white space is ignored.
If \"unsigned=true\", a Real or Integer literal
is not allowed to start with a \"+\" or \"-\" sign.
</p>
<h4>Example</h4>
<blockquote><pre>
import Modelica.Utilities.Strings;
import T = Modelica.Utilities.Types.TokenType;
(token, index) := Strings.scanToken(string);
<strong>if</strong> token.tokenType == T.RealToken <strong>then</strong>
realValue := token.real;
<strong>elseif</strong> token.tokenType == T.IntegerToken <strong>then</strong>
integerValue := token.integer;
<strong>elseif</strong> token.tokenType == T.BooleanToken <strong>then</strong>
booleanValue := token.boolean;
<strong>elseif</strong> token.tokenType == T.Identifier <strong>then</strong>
name := token.string;
<strong>else</strong>
Strings.syntaxError(string,index,\"Expected Real, Integer, Boolean or identifier token\");
<strong>end if</strong>;
</pre></blockquote>
</html>"));
end scanToken;
function scanReal
"Scan for the next Real number and trigger an assert if not present"
extends Modelica.Icons.Function;
input String string "String to be scanned";
input Integer startIndex(min=1)=1
"Start scanning of string at character startIndex";
input Boolean unsigned=false
"= true, if Real token shall not start with a sign";
input String message=""
"Message used in error message if scan is not successful";
output Real number "Value of real number";
output Integer nextIndex "Index of character after the found number";
algorithm
(nextIndex, number) :=Advanced.scanReal(string, startIndex, unsigned);
if nextIndex == startIndex then
nextIndex :=Advanced.skipWhiteSpace(string, startIndex);
if unsigned then
syntaxError(string, nextIndex, "Expected a Real number without a sign " + message);
else
syntaxError(string, nextIndex, "Expected a Real number " + message);
end if;
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
number = Strings.<strong>scanReal</strong>(string);
(number, nextIndex) = Strings.<strong>scanReal</strong>(string, startIndex=1,
unsigned=false, message=\"\");
</pre></blockquote>
<h4>Description</h4>
<p>
The first form, \"scanReal(string)\", scans \"string\" for a
Real number with leading white space and returns the value.
</p>
<p>
The second form, \"scanReal(string,startIndex,unsigned)\",
scans the string starting at index
\"startIndex\", checks whether the next token is a Real literal
and returns its value as a Real number, as well as the
index directly after the Real number.
If the optional argument \"unsigned\" is <strong>true</strong>,
the real number shall not have a leading \"+\" or \"-\" sign.
</p>
<p>
If the required Real number with leading white space
is not present in \"string\", an assert is triggered.
</p>
</html>"));
end scanReal;
function scanInteger
"Scan for the next Integer number and trigger an assert if not present"
extends Modelica.Icons.Function;
input String string "String to be scanned";
input Integer startIndex(min=1)=1
"Start scanning of string at character startIndex";
input Boolean unsigned=false
"= true, if Integer token shall not start with a sign";
input String message=""
"Message used in error message if scan is not successful";
output Integer number "Value of Integer number";
output Integer nextIndex "Index of character after the found number";
algorithm
(nextIndex, number) :=Advanced.scanInteger(string, startIndex, unsigned);
if nextIndex == startIndex then
nextIndex :=Advanced.skipWhiteSpace(string, startIndex);
if unsigned then
syntaxError(string, nextIndex, "Expected an Integer number without a sign " + message);
else
syntaxError(string, nextIndex, "Expected an Integer number " + message);
end if;
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
number = Strings.<strong>scanInteger</strong>(string);
(number, nextIndex) = Strings.<strong>scanInteger</strong>(string, startIndex=1,
unsigned=false, message=\"\");
</pre></blockquote>
<h4>Description</h4>
<p>
Function <strong>scanInteger</strong> scans the string starting at index
\"startIndex\", checks whether the next token is an Integer literal
and returns its value as an Integer number, as well as the
index directly after the Integer number. An assert is triggered,
if the scanned string does not contain an Integer literal with optional
leading white space.
</p>
</html>"));
end scanInteger;
function scanBoolean
"Scan for the next Boolean number and trigger an assert if not present"
extends Modelica.Icons.Function;
input String string "String to be scanned";
input Integer startIndex(min=1)=1
"Start scanning of string at character startIndex";
input String message=""
"Message used in error message if scan is not successful";
output Boolean number "Value of Boolean";
output Integer nextIndex "Index of character after the found number";
protected
String identifier;
algorithm
(nextIndex, identifier) :=Advanced.scanIdentifier(string, startIndex);
if nextIndex > startIndex then
if identifier == "false" then
number := false;
elseif identifier == "true" then
number := true;
else
nextIndex := startIndex;
end if;
end if;
if nextIndex == startIndex then
nextIndex :=Advanced.skipWhiteSpace(string, startIndex);
syntaxError(string, nextIndex,
"Expected a Boolean constant, i.e., \"false\" or \"true\" " + message);
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
number = Strings.<strong>scanBoolean</strong>(string);
(number, nextIndex) = Strings.<strong>scanBoolean</strong>(string, startIndex=1, message=\"\");
</pre></blockquote>
<h4>Description</h4>
<p>
Function <strong>scanBoolean</strong> scans the string starting at index
\"startIndex\", checks whether the next token is a Boolean literal
(i.e., is either the string \"false\" or \"true\", if converted to lower case letters)
and returns its value as a Boolean number, as well as the
index directly after the Boolean number. An assert is triggered,
if the scanned string does not contain a Boolean literal with optional
leading white space.
</p>
</html>"));
end scanBoolean;
function scanString
"Scan for the next Modelica string and trigger an assert if not present"
extends Modelica.Icons.Function;
input String string "String to be scanned";
input Integer startIndex(min=1)=1
"Start scanning of string at character startIndex";
input String message=""
"Message used in error message if scan is not successful";
output String result "Value of string";
output Integer nextIndex "Index of character after the found string";
algorithm
(nextIndex, result) :=Advanced.scanString(string, startIndex);
if nextIndex == startIndex then
nextIndex :=Advanced.skipWhiteSpace(string, startIndex);
syntaxError(string, nextIndex, "Expected a string enclosed in double quotes " + message);
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
string2 = Strings.<strong>scanString</strong>(string);
(string2, nextIndex) = Strings.<strong>scanString</strong>(string, startIndex=1, message=\"\");
</pre></blockquote>
<h4>Description</h4>
<p>
Function <strong>scanString</strong> scans the string starting at index
\"startIndex\", checks whether the next token is a String literal
and returns its value as a String, as well as the
index directly after the String. An assert is triggered,
if the scanned string does not contain a String literal with optional
leading white space.
</p>
</html>"));
end scanString;
function scanIdentifier
"Scan for the next Identifier and trigger an assert if not present"
extends Modelica.Icons.Function;
input String string "String to be scanned";
input Integer startIndex(min=1)=1
"Start scanning of identifier at character startIndex";
input String message=""
"Message used in error message if scan is not successful";
output String identifier "Value of Identifier";
output Integer nextIndex "Index of character after the found identifier";
algorithm
(nextIndex, identifier) :=Advanced.scanIdentifier(string, startIndex);
if nextIndex == startIndex then
nextIndex :=Advanced.skipWhiteSpace(string, startIndex);
syntaxError(string, nextIndex, "Expected an identifier " + message);
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
identifier = Strings.<strong>scanIdentifier</strong>(string);
(identifier, nextIndex) = Strings.<strong>scanIdentifier</strong>(string, startIndex=1, message=\"\");
</pre></blockquote>
<h4>Description</h4>
<p>
Function <strong>scanIdentifier</strong> scans the string starting at index
\"startIndex\", checks whether the next token is an Identifier
and returns its value as a string, as well as the
index directly after the Identifier. An assert is triggered,
if the scanned string does not contain an Identifier with optional
leading white space.
</p>
</html>"));
end scanIdentifier;
function scanDelimiter
"Scan for the next delimiter and trigger an assert if not present"
extends Modelica.Icons.Function;
input String string "String to be scanned";
input Integer startIndex(min=1)=1
"Start scanning of delimiters at character startIndex";
input String requiredDelimiters[:]={","} "Delimiters that are searched";
input String message=""
"Message used in error message if scan is not successful";
output String delimiter "Found delimiter";
output Integer nextIndex "Index of character after the found delimiter";
protected
Integer lenString = length(string);
Integer lenDelimiter;
Integer nDelimiters = size(requiredDelimiters,1);
Integer endIndex;
Boolean found;
Integer i;
encapsulated function concatenate "Concatenate strings together"
import Modelica;
extends Modelica.Icons.Function;
input String strings[:];
output String string;
algorithm
string := "{\"";
for i in 1:size(strings,1) loop
if i == 1 then
string := "{\"" + strings[1] + "\"";
else
string := string + ", \"" + strings[i] + "\"";
end if;
end for;
string := string + "}";
end concatenate;
algorithm
nextIndex := Advanced.skipLineComments(string,startIndex);
found := false;
i := 1;
while not found and i <= nDelimiters loop
lenDelimiter :=length(requiredDelimiters[i]);
if lenDelimiter == 0 then
found := true;
delimiter := "";
else
endIndex :=nextIndex + lenDelimiter - 1;
if endIndex <= lenString then
if substring(string,nextIndex,endIndex) == requiredDelimiters[i] then
found := true;
delimiter := requiredDelimiters[i];
end if;
end if;
i := i + 1;
end if;
end while;
if found then
nextIndex := nextIndex + lenDelimiter;
else
if size(requiredDelimiters,1) == 1 then
syntaxError(string, nextIndex, "Expected delimiter \"" + requiredDelimiters[1] + "\"\n" + message);
else
syntaxError(string, nextIndex, "Expected a delimiter out of " +
concatenate(requiredDelimiters) + "\n" + message);
end if;
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
delimiter = Strings.<strong>scanDelimiter</strong>(string);
(delimiter, nextIndex) = Strings.<strong>scanDelimiter</strong>(string, startIndex=1,
requiredDelimiters={\",\"}, message=\"\");
</pre></blockquote>
<h4>Description</h4>
<p>
Function <strong>scanDelimiter</strong> scans the string starting at index
\"startIndex\", checks whether the next token is a delimiter string
and returns its value as a string, as well as the
index directly after the delimiter. An assert is triggered,
if the scanned string does not contain a delimiter out of the
list of requiredDelimiters. Input argument requiredDelimiters is a vector
of strings. The elements may have any length, including length 0.
If an element of the requiredDelimiters is zero, white space
is treated as delimiter. The function returns delimiter=\"\" and nextIndex
is the index of the first non white space character.
</p>
</html>"));
end scanDelimiter;
function scanNoToken "Scan string and check that it contains no more token"
extends Modelica.Icons.Function;
input String string "String to be scanned";
input Integer startIndex(min=1)=1
"Start scanning of string at character startIndex";
input String message=""
"Message used in error message if scan is not successful";
protected
Integer nextIndex;
algorithm
nextIndex :=Advanced.skipLineComments(string, startIndex);
if nextIndex <= length(string) then
syntaxError(string, nextIndex, "Expected no more token " + message);
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Strings.<strong>scanNoToken</strong>(string, startIndex=1, message=\"\");
</pre></blockquote>
<h4>Description</h4>
<p>
Function <strong>scanNoToken</strong> scans the string starting at index
\"startIndex\" and checks whether there is no more token in the
string. An assert is triggered if this is not the case,
using the \"message\" argument as additional explanation in
the error text.
</p>
</html>"));
end scanNoToken;
function syntaxError
"Print an error message, a string and the index at which scanning detected an error"
extends Modelica.Icons.Function;
input String string "String that has an error at position index";
input Integer index "Index of string at which scanning detected an error";
input String message="" "String printed at end of error message";
protected
Integer maxIndex = 40;
Integer maxLenString = 60;
Integer lenString = length(string);
String errString;
Integer index2 = if index < 1 then 1 else if index > lenString then lenString else index;
algorithm