/
String.prg
1130 lines (962 loc) · 33.9 KB
/
String.prg
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
//
// Copyright (c) XSharp B.V. All Rights Reserved.
// Licensed under the Apache License, Version 2.0.
// See License.txt in the project root for license information.
//
USING XSharp
USING System.Text
// Array of chars used for the various trim functions
INTERNAL GLOBAL trimChars := <CHAR>{ ' ' } AS CHAR[]
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/alltrim/*" />
/// <returns>A trimmed string, with leading and trailing spaces removed.</returns>
FUNCTION AllTrim(cString AS STRING) AS STRING
IF ( cString == NULL )
RETURN cString
ENDIF
RETURN cString:Trim(trimChars)
INTERNAL FUNCTION __AscWorker(cString AS STRING, lMustBeAnsi AS LOGIC) AS DWORD
LOCAL ascValue := 0 AS DWORD
LOCAL chValue AS CHAR
IF ( !String.IsNullOrEmpty(cString) )
chValue := cString[0]
ascValue := (DWORD) chValue
IF ascValue > 127
LOCAL encoding AS Encoding
IF lMustBeAnsi .or. RuntimeState.Ansi
encoding := StringHelpers.WinEncoding
ELSE
encoding := StringHelpers.DosEncoding
ENDIF
LOCAL buffer AS BYTE[]
VAR chars := <CHAR> {chValue}
IF encoding:IsSingleByte
buffer := BYTE[]{1}
encoding:GetBytes(chars,0,1,buffer,0)
ascValue := buffer[0]
ELSE
buffer := BYTE[]{2}
IF encoding:GetBytes(chars,0,1,buffer,0) == 1
ascValue := buffer[0]
ELSE
IF BitConverter.IsLittleEndian
LOCAL tmp := buffer[0] AS BYTE
buffer[0] := buffer[1]
buffer[1] := tmp
ENDIF
ascValue := BitConverter.ToUInt16( buffer, 0 )
ENDIF
ENDIF
ENDIF
ENDIF
RETURN ascValue
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/asc/*" />
/// <remarks>
/// AscA() always uses the current Ansi codepage, and ignores the setting of SetAnsi()
/// </remarks>
/// <seealso cref='O:XSharp.Core.Functions.Asc'>Asc</seealso>
/// <seealso cref='O:XSharp.Core.Functions.AscW'>AscW</seealso>
/// <seealso cref='O:XSharp.Core.Functions.ChrA'>ChrA</seealso>
FUNCTION AscA(cString AS STRING) AS DWORD
RETURN __AscWorker(cString, TRUE)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/asc/*" />
/// <remarks>
/// The return value of Asc() in XSharp depends on the setting of SetAnsi().<br/>
/// When SetAnsi() = TRUE then the active windows <b>Ansi</b> codepage is used to calculate the result.<br/>
/// When SetAnsi() = FALSE then the active windows <b>Oem</b> codepage is used to calculate the result.<br/>
/// This is different from the behior in most single byte versions of Xbase where Asc() simply returnes the
/// byte value of the 1st character of the string.
/// </remarks>
/// <seealso cref='O:XSharp.Core.Functions.AscA'>AscA</seealso>
/// <seealso cref='O:XSharp.Core.Functions.AscW'>AscW</seealso>
FUNCTION Asc(cString AS STRING) AS DWORD
RETURN __AscWorker(cString, FALSE)
/// <summary>
/// Convert a character to its Unicode ASCII value.
/// </summary>
/// <param name="c"></param>
/// <returns>Number of the Unicode character</returns>
/// <seealso cref='O:XSharp.Core.Functions.Asc'>Asc</seealso>
/// <seealso cref='O:XSharp.Core.Functions.AscA'>AscA</seealso>
/// <seealso cref='O:XSharp.Core.Functions.ChrW'>ChrW</seealso>
FUNCTION AscW(c AS STRING) AS DWORD
LOCAL ascValue := 0 AS DWORD
LOCAL chValue AS CHAR
IF ( !String.IsNullOrEmpty(c) )
chValue := c[0]
ascValue := (DWORD) chValue
ENDIF
RETURN ascValue
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/at/*" />
FUNCTION At(cSearch AS STRING,cTarget AS STRING) AS DWORD
LOCAL position := 0 AS DWORD
IF ( cTarget != NULL .AND. cSearch != NULL )
IF cTarget:Length != 0 .AND. cSearch:Length != 0
position := (DWORD) cTarget:IndexOf(cSearch, StringComparison.Ordinal) +1
END IF
ENDIF
RETURN position
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/at2/*" />
FUNCTION At2(cSearch AS STRING,cTarget AS STRING) AS DWORD
RETURN At(cSearch,cTarget)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/at3/*" />
FUNCTION At3(cSearch AS STRING,cTarget AS STRING,dwOffset AS DWORD) AS DWORD
LOCAL position := 0 AS DWORD
// note dwOffset is ZERO based in VO
IF ( cTarget != NULL .AND. cSearch != NULL .AND. dwOffset <= cTarget:Length )
IF cTarget:Length != 0 .AND. cSearch:Length != 0
position := (DWORD) cTarget:IndexOf(cSearch,(INT)dwOffset, StringComparison.Ordinal) +1
ENDIF
ENDIF
RETURN position
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/atc/*" />
FUNCTION AtC(cSearch AS STRING,cTarget AS STRING) AS DWORD
LOCAL position := 0 AS DWORD
IF ( cTarget != NULL .AND. cSearch != NULL )
IF cTarget:Length != 0 .AND. cSearch:Length != 0
position := (DWORD) cTarget:IndexOf(cSearch, StringComparison.OrdinalIgnoreCase) +1
ENDIF
ENDIF
RETURN position
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/atc2/*" />
FUNCTION AtC2(cSearch AS STRING,cTarget AS STRING) AS DWORD
RETURN AtC(cSearch,cTarget)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/atcline/*" />
FUNCTION ATCLine(cSearch AS STRING,cTarget AS STRING) AS DWORD
RETURN ATLine( cSearch:ToUpper(), cTarget:ToUpper() )
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/atcline2/*" />
FUNCTION ATCLine2(cSearch AS STRING,cTarget AS STRING) AS DWORD
RETURN ATLine2( cSearch:ToUpper(), cTarget:ToUpper() )
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/atline/*" />
FUNCTION ATLine(cSearch AS STRING,cTarget AS STRING) AS DWORD
LOCAL nPos AS DWORD
IF String.IsNullOrEmpty(cTarget) .OR. String.IsNullOrEmpty(cSearch)
RETURN 0
ENDIF
IF cTarget:StartsWith(cSearch)
RETURN 1
ENDIF
nPos := At( cSearch, cTarget )
IF (nPos > 0)
cTarget := Left( cTarget, nPos - 1 )
nPos := MemLines( cTarget)
IF cTarget:EndsWith(RuntimeState.Eol)
nPos++
ENDIF
ENDIF
RETURN nPos
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/atline2/*" />
FUNCTION ATLine2(cSearch AS STRING,cTarget AS STRING) AS DWORD
RETURN ATLine(cSearch, cTarget)
/// <summary>This function is not implemented yet</summary>
/// <param name="c"></param>
/// <returns>
/// </returns>
FUNCTION B64EncFile(c AS STRING) AS STRING
THROW NotImplementedException{}
//RETURN String.Empty
/// <summary>This function is not implemented yet</summary>
/// <param name="cIn"></param>
/// <returns>
/// </returns>
FUNCTION B64EncString(cIn AS STRING) AS STRING
THROW NotImplementedException{}
//RETURN String.Empty
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/buffer/*" />
FUNCTION Buffer(dwSize AS DWORD) AS STRING
RETURN STRING{'\0', (INT) dwSize}
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/chareven/*" />
FUNCTION CharEven(cString AS STRING) AS STRING
LOCAL evenChars:=NULL AS STRING
IF ( !String.IsNullOrEmpty(cString) )
//local chars := c:ToCharArray() as char[]
LOCAL isEven := FALSE AS LOGIC
LOCAL sb := System.Text.StringBuilder{} AS System.Text.StringBuilder
FOREACH ch AS char IN cString
IF isEven
sb:Append(ch)
ENDIF
isEven := !isEven
NEXT
evenChars := sb:ToString()
ENDIF
RETURN evenChars
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/charmix/*" />
FUNCTION CharMix(cOdd AS STRING,cEven AS STRING) AS STRING
LOCAL n1 := 0 AS INT
LOCAL n2 := 0 AS INT
LOCAL i1 := 0 AS INT
LOCAL i2 := 0 AS INT
LOCAL sb AS StringBuilder
IF cEven:Length == 0
RETURN ""
ELSE
sb := StringBuilder{ cOdd:Length * 2 }
n1 := cOdd:Length - 1
n2 := cEven:Length - 1
FOR i1 := 0 UPTO n1
sb:Append( cOdd[i1] )
IF i2 > n2
i2 := 0
ENDIF
sb:Append( cEven[i2++] )
NEXT
RETURN sb:ToString()
ENDIF
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/charodd/*" />
FUNCTION CharOdd(cString AS STRING) AS STRING
LOCAL oddChars:=NULL AS STRING
IF ( !String.IsNullOrEmpty(cString) )
//local chars := c:ToCharArray() as char[]
LOCAL isOdd := TRUE AS LOGIC
LOCAL sb := System.Text.StringBuilder{} AS System.Text.StringBuilder
FOREACH ch AS char IN cString
IF isOdd
sb:Append(ch)
ENDIF
isOdd := !isOdd
NEXT
oddChars := sb:ToString()
ENDIF
RETURN oddChars
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/charpos/*" />
FUNCTION CharPos(cString AS STRING, wPosition AS DWORD) AS STRING
LOCAL result := String.Empty AS STRING
IF ( wPosition >= 1 .AND. wPosition <= (DWORD) cString:Length )
result := cString:Substring((INT)wPosition-1,1)
ENDIF
RETURN result
INTERNAL FUNCTION __ChrWorker(dwCode AS DWORD, lMustBeAnsi AS LOGIC) AS STRING
LOCAL b AS BYTE
LOCAL ret AS STRING
b := (BYTE)( dwCode & 0xFF ) // VO ignores the high 24 bits
IF b <= 0x7F
ret := Convert.ToChar( b ):ToString()
ELSE
LOCAL encoding AS Encoding
IF lMustBeAnsi .or. RuntimeState.Ansi
encoding := StringHelpers.WinEncoding
ELSE
encoding := StringHelpers.DosEncoding
ENDIF
LOCAL chars := CHAR[]{ 1 } AS CHAR[]
LOCAL bytes := BYTE[]{ 1 } AS BYTE[]
LOCAL decoder := encoding:GetDecoder() AS Decoder
bytes[__ARRAYBASE__] := b
decoder:GetChars( bytes, 0, 1, chars, 0 )
ret := chars[__ARRAYBASE__]:ToString()
ENDIF
RETURN ret
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/chr/*" />
/// <remarks>
/// The value of dwChar must be between 0 and 255<br/>
/// The return value of Chr() in XSharp depends on the setting of SetAnsi().<br/>
/// When SetAnsi() = TRUE then the active windows <b>Ansi</b> codepage is used to calculate the character.<br/>
/// When SetAnsi() = FALSE then the active windows <b>Oem</b> codepage is used to calculate the character.<br/>
/// This is different from the behior in most single byte versions of Xbase where Chr() simply returnes a string with a single byte
/// that matches the number passed to this function.
/// </remarks>
FUNCTION Chr(dwCode AS DWORD) AS STRING
RETURN __ChrWorker(dwCode, FALSE)
/// <summary>
/// Convert an ASCII code to a character value, always using the Ansi codepage, ignoring the SetAnsi() setting.
/// </summary>
/// <param name="dwCode"></param>
/// <returns>
/// </returns>
/// <seealso cref='O:XSharp.Core.Functions.AscA'>AscA</seealso>
FUNCTION ChrA(dwCode AS DWORD) AS STRING
RETURN __ChrWorker(dwCode, TRUE)
/// <summary>
/// Convert an ASCII code to a character value.
/// </summary>
/// <param name="dwChar"></param>
/// <returns>
/// </returns>
/// <seealso cref='O:XSharp.Core.Functions.AscW'>AscW</seealso>
FUNCTION ChrW(dwChar AS DWORD) AS STRING
IF dwChar > 0xFFFF
THROW Error.ArgumentError( __ENTITY__, nameof(dwChar), "Number too High")
ENDIF
RETURN Convert.ToChar( (INT) ( dwChar & 0xFFFF ) ):ToString()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/decodebase64/*" />
FUNCTION DecodeBase64(cAttachment AS STRING,hDestination AS IntPtr) AS INT
IF cAttachment == NULL .OR. cAttachment:Length== 0
RETURN 0
ENDIF
VAR aBytes := Convert.FromBase64String( cAttachment )
RETURN (INT) FWrite3(hDestination, aBytes, (DWORD) aBytes:Length)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/encodebase64/*" />
FUNCTION EncodeBase64(hSource AS IntPtr,hDestination AS IntPtr) AS INT
LOCAL nSize AS DWORD
LOCAL nRead AS DWORD
// determine file size
FSeek3(hSource, 0, FS_END)
nSize := FTell(hSource)
FSeek3(hSource, 0, FS_SET)
VAR aBuffer := BYTE[]{ (INT) nSize}
nRead := FRead3(hSource, aBuffer, nSize)
IF nRead != nSize
RETURN 0
ENDIF
VAR cContents := Convert.ToBase64String(aBuffer, Base64FormattingOptions.InsertLineBreaks)
nSize := FWrite(hDestination, cContents)
RETURN (INT) nSize
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/hardcr/*" />
FUNCTION HardCR(cString AS STRING) AS STRING
RETURN cString:Replace( (CHAR) 141, (CHAR) 13 )
INTERNAL FUNCTION _nibble (c AS CHAR) AS BYTE
LOCAL b AS BYTE
SWITCH c
CASE '0'
CASE '1'
CASE '2'
CASE '3'
CASE '4'
CASE '5'
CASE '6'
CASE '7'
CASE '8'
CASE '9'
b := (BYTE) (c - '0')
CASE 'A'
CASE 'B'
CASE 'C'
CASE 'D'
CASE 'E'
CASE 'F'
b := (BYTE) (c - 'A' + 10)
CASE 'a'
CASE 'b'
CASE 'c'
CASE 'd'
CASE 'e'
CASE 'f'
b := (BYTE) (c - 'a' + 10)
OTHERWISE
b := 0
END SWITCH
RETURN b
/// <summary>
/// </summary>
/// <param name="c"></param>
/// <returns>
/// </returns>
FUNCTION Hex2C(c AS STRING) AS STRING
LOCAL i AS INT
LOCAL sb AS StringBuilder
sb := StringBuilder{c:Length}
i := 0
DO WHILE i <= c:Length - 2
VAR b1 := _nibble(c[i])
VAR b2 := _nibble(c[i+1])
VAR b := (b1 << 4) + b2
sb:Append( Chr( (DWORD) b))
i += 2
ENDDO
RETURN sb:ToString()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/instr/*" />
FUNCTION Instr(cSearch AS STRING,cTarget AS STRING) AS LOGIC
LOCAL result := FALSE AS LOGIC
IF cSearch != NULL .AND. cTarget != NULL .AND. cSearch:Length != 0
result := cTarget:IndexOf( cSearch, StringComparison.Ordinal ) > -1
ENDIF
RETURN result
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/left/*" />
FUNCTION Left(cString AS STRING, dwCount AS DWORD) AS STRING
IF ( cString!=NULL )
IF dwCount < cString:Length
cString := cString:Substring( 0, (INT) dwCount )
ELSE
NOP
ENDIF
ENDIF
RETURN cString
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/lower/*" />
FUNCTION Lower(cString AS STRING) AS STRING
IF cString != NULL
cString := cString:ToLower()
ENDIF
RETURN cString
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/lowera/*" />
FUNCTION LowerA(cString REF STRING) AS STRING
IF cString != NULL
cString := cString:ToLower()
ENDIF
RETURN cString
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/ltrim/*" />
FUNCTION LTrim(cString AS STRING) AS STRING
IF (cString == NULL)
RETURN cString
ENDIF
RETURN cString:TrimStart(trimChars)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/occurs/*" />
FUNCTION Occurs(cSearch AS STRING,cTarget AS STRING) AS DWORD
RETURN Occurs3(cSearch,cTarget, 0)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/occurs2/*" />
FUNCTION Occurs2(cSearch AS STRING,cTarget AS STRING) AS DWORD
RETURN Occurs3(cSearch,cTarget, 0)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/occurs3/*" />
FUNCTION Occurs3(cSearch AS STRING,cTarget AS STRING,dwOffset AS DWORD) AS DWORD
LOCAL pos AS INT
LOCAL count AS DWORD
IF String.IsNullOrEmpty(cSearch) .OR. String.IsNullOrEmpty(cTarget)
RETURN 0
ENDIF
IF dwOffset > 0
dwOffset -= 1
ENDIF
count := 0
IF dwOffset < (DWORD) cTarget:Length
DO WHILE ( pos := cTarget:IndexOf(cSearch, (INT)dwOffset, StringComparison.Ordinal) ) >= 0
count++
dwOffset := (DWORD)(pos + cSearch:Length)
ENDDO
ENDIF
RETURN count
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/ansi2oem/*" />
/// <remarks><include file="CoreComments.xml" path="Comments/ansi2oem/*" /></remarks>
/// <seealso cref="Ansi2OemBuff" />
FUNCTION Ansi2Oem(cAnsiString AS STRING) AS STRING
LOCAL aBytes AS BYTE[]
LOCAL iLen AS INT
iLen := cAnsiString:Length
aBytes := String2Bytes(cAnsiString)
aBytes := Ansi2Oem(aBytes, iLen)
RETURN Bytes2String(aBytes, iLen)
/// <summary>
/// Convert an array of bytes from ANSI to OEM.
/// </summary>
/// <param name="bSource">String in Ansi format</param>
// <inheritdoc cref="Ansi2Oem" />
/// <inheritdoc cref="Ansi2Oem" />
FUNCTION Ansi2Oem(bSource AS BYTE[]) AS BYTE[]
RETURN Ansi2Oem(bSource, bSource:Length)
/// <inheritdoc cref="Ansi2Oem(System.Byte[])" />
/// <param name="iLen">Length of the source array</param>
FUNCTION Ansi2Oem(bSource AS BYTE[], iLen AS INT) AS BYTE[]
LOCAL bDest AS BYTE[]
bDest := BYTE[]{iLen}
CharToOemBuffA(bSource, bDest, (DWORD) iLen)
RETURN bDest
/// <overloads>
/// <remarks><include file="CoreComments.xml" path="Comments/ansi2oem/*" /></remarks>
/// <seealso cref="Oem2AnsiA" />
/// <seealso cref="Ansi2Oem" />
/// </overloads>
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/ansi2oema/*" />
/// <remarks><include file="CoreComments.xml" path="Comments/ansi2oem/*" /></remarks>
/// <seealso cref="M:Ansi2OemBuff" />
FUNCTION Ansi2OemA(cAnsiString REF STRING) AS STRING
LOCAL aBytes AS BYTE[]
LOCAL iLen AS INT
iLen := cAnsiString:Length
aBytes := String2Bytes(cAnsiString)
aBytes := Ansi2Oem(aBytes, iLen)
cAnsiString := Bytes2String(aBytes, iLen)
RETURN cAnsiString
/// <summary>
/// Convert an array of ANSI characters to OEM characters, changing the contents of the argument as well as the return value.
/// </summary>
/// <param name="bSource">A byte array that contains the string to convert</param>
/// <inheritdoc cref="M:Ansi2OemA(System.String@)" />
FUNCTION Ansi2OemA(bSource AS BYTE[]) AS VOID
LOCAL bDest AS BYTE[]
bDest := Ansi2Oem(bSource, bSource:Length)
System.Array.Copy(bDest, bSource, bSource:Length)
RETURN
/// <overloads>
/// <remarks><include file="CoreComments.xml" path="Comments/ansi2oem/*" /></remarks>
/// <seealso cref="Ansi2Oem" />
/// <seealso cref="Oem2AnsiA" />
/// </overloads>
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/oem2ansi/*" />
/// <remarks><include file="CoreComments.xml" path="Comments/ansi2oem/*" /></remarks>
/// <seealso cref="M:Oem2AnsiBuff" />
FUNCTION Oem2Ansi(cOemString AS STRING) AS STRING
LOCAL aBytes AS BYTE[]
LOCAL iLen AS INT
iLen := cOemString:Length
aBytes := String2Bytes(cOemString)
aBytes := Oem2Ansi(aBytes, iLen)
RETURN Bytes2String(aBytes, iLen)
/// <summary>
/// Convert an array of OEM characters to ANSI characters.
/// </summary>
/// <param name="bSource">A byte array that contains the string to convert</param>
/// <inheritdoc cref="M:Oem2Ansi(System.String)" />
FUNCTION Oem2Ansi(bSource AS BYTE[]) AS BYTE[]
RETURN Oem2Ansi(bSource, bSource:Length)
/// <overloads>
/// <remarks><include file="CoreComments.xml" path="Comments/ansi2oem/*" /></remarks>
/// <seealso cref="Oem2Ansi" />
/// <seealso cref="Ansi2OemA" />
/// </overloads>
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/oem2ansia/*" />
/// <remarks><include file="CoreComments.xml" path="Comments/ansi2oem/*" /></remarks>
FUNCTION Oem2AnsiA(cOemString REF STRING) AS STRING
LOCAL aBytes AS BYTE[]
LOCAL iLen AS INT
iLen := cOemString:Length
aBytes := String2Bytes(cOemString)
aBytes := Oem2Ansi(aBytes, iLen)
cOemString := Bytes2String(aBytes, iLen)
RETURN cOemString
/// <param name="bSource">A byte array that contains the string to convert</param>
/// <inheritdoc cref="System.String@)" />
FUNCTION Oem2AnsiA(bSource AS BYTE[]) AS VOID
LOCAL bDest AS BYTE[]
bDest := Oem2Ansi(bSource, bSource:Length)
System.Array.Copy(bDest, bSource, bSource:Length)
RETURN
/// <inheritdoc cref="Oem2Ansi(System.Byte[])" />
/// <param name="iLen">The number of characters to convert</param>
/// <returns>String converted to Ansi</returns>
/// <seealso cref="Ansi2Oem" />
FUNCTION Oem2Ansi(bSource AS BYTE[], iLen AS INT) AS BYTE[]
LOCAL bDest AS BYTE[]
bDest := BYTE[]{iLen}
OemToCharBuffA(bSource, bDest, (DWORD) iLen)
RETURN bDest
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/oem2ansibuff/*" />
/// <remarks><include file="CoreComments.xml" path="Comments/ansi2oem/*" /></remarks>
/// <seealso cref="Ansi2OemBuff" />
FUNCTION Oem2AnsiBuff(pszTarget AS BYTE[],pszSource AS BYTE[],dwCount AS DWORD) AS BYTE[]
OemToCharBuffA(pszSource, pszTarget, dwCount)
RETURN pszTarget
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/ansi2oembuff/*" />
/// <remarks><include file="CoreComments.xml" path="Comments/ansi2oem/*" /></remarks>
/// <seealso cref="Oem2AnsiBuff" />
FUNCTION Ansi2OemBuff(pszTarget AS BYTE[],pszSource AS BYTE[],dwCount AS DWORD) AS BYTE[]
CharToOemBuffA(pszSource, pszTarget, dwCount)
RETURN pszTarget
INTERNAL _DLL FUNCTION CharToOemBuffA( lpszSrc AS BYTE[], lpszDst AS BYTE[], cchDstLength AS DWORD ) AS LOGIC PASCAL:USER32.CharToOemBuffA
INTERNAL _DLL FUNCTION OemToCharBuffA( lpszSrc AS BYTE[], lpszDst AS BYTE[], cchDstLength AS DWORD ) AS LOGIC PASCAL:USER32.OemToCharBuffA
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/proper/*" />
FUNCTION Proper(cText AS STRING) AS STRING
LOCAL sb AS StringBuilder
LOCAL inside AS LOGIC
IF cText != NULL
sb := StringBuilder{cText:Length}
inside := FALSE
FOREACH ch AS CHAR IN cText
VAR cToAdd := ch
IF inside
IF Char.IsLetterOrDigit(ch)
cToAdd := Char.ToLower(ch)
ELSE
inside := FALSE
ENDIF
ELSE
IF Char.IsLetterOrDigit(ch)
cToAdd := Char.ToUpper(ch)
inside := TRUE
ENDIF
ENDIF
sb:Append(cToAdd)
NEXT
cText := sb:ToString()
ENDIF
RETURN cText
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/propera/*" />
FUNCTION ProperA(cName REF STRING) AS STRING
cName := Proper(cName)
RETURN cName
/// <summary>
/// </summary>
/// <param name="cIn"></param>
/// <returns>
/// </returns>
FUNCTION QPEncString(cIn AS STRING) AS STRING
THROW NotImplementedException{}
//RETURN String.Empty
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/rat/*" />
FUNCTION RAt(cSearch AS STRING,cTarget AS STRING) AS DWORD
LOCAL rightMost := 0 AS DWORD
IF cSearch != NULL .AND. cTarget != NULL
IF cTarget:Length != 0 .AND. cSearch:Length != 0
rightMost:= (DWORD) cTarget:LastIndexOf(cSearch, StringComparison.Ordinal) + 1
ENDIF
ENDIF
RETURN rightMost
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/rat/*" />
FUNCTION RAt2(cSearch AS STRING,cTarget AS STRING) AS DWORD
RETURN RAt(cSearch,cTarget)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/rat3/*" />
FUNCTION RAt3(cSearch AS STRING,cTarget AS STRING,dwOffSet AS DWORD) AS DWORD
LOCAL nResult := 0 AS DWORD
IF cSearch != NULL .AND. cTarget != NULL
IF cTarget:Length != 0 .AND. cSearch:Length != 0
IF dwOffSet > (DWORD) cTarget:Length
dwOffSet := 0U
ENDIF
VAR cTemp := cTarget:Substring((INT) dwOffSet)
nResult := RAt(cSearch, cTemp)
IF nResult > 0U
nResult := nResult+dwOffSet
ENDIF
ENDIF
ENDIF
RETURN nResult
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/ratline/*" />
FUNCTION RAtLine(cSearch AS STRING, cTarget AS STRING) AS DWORD
LOCAL nPos AS DWORD
IF cSearch == NULL .OR. cTarget == NULL .OR. cSearch:Length == 0 .OR. cTarget:Length == 0
RETURN 0
ENDIF
nPos := RAt(cSearch,cTarget)
cTarget := Left(cTarget,nPos-1)
RETURN MemLines(cTarget)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/ratline2/*" />
FUNCTION RATLine2(cSearch AS STRING,cTarget AS STRING) AS DWORD
RETURN RAtLine(cSearch, cTarget)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/replicate/*" />
FUNCTION Repl(cString AS STRING,dwCount AS DWORD) AS STRING
RETURN Replicate(cString, dwCount)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/replicate/*" />
FUNCTION Replicate(cString AS STRING,dwCount AS DWORD) AS STRING
LOCAL cReturn := "" AS STRING
IF dwCount > 0 .AND. cString != NULL
cReturn := System.Text.StringBuilder{}:Insert( 0, cString, (INT) dwCount ):ToString()
ENDIF
RETURN cReturn
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/right/*" />
FUNCTION Right(cString AS STRING,dwCount AS DWORD) AS STRING
IF cString != NULL
IF dwCount > cString:Length
NOP
ELSE
cString := cString:Substring( cString:Length - (INT) dwCount, (INT) dwCount )
ENDIF
ENDIF
RETURN cString
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/rtrim/*" />
FUNCTION RTrim(cString AS STRING) AS STRING
IF ( cString == NULL )
RETURN cString
ENDIF
RETURN cString:TrimEnd(trimChars)
FUNCTION SClone(cString AS STRING) AS STRING
LOCAL clonedString := NULL AS STRING
IF (cString != NULL)
// String.Copy is not supported on modern .Net
#ifdef NETNEXT
VAR sb := System.Text.StringBuilder{}
sb:Append(cString)
clonedString := sb:ToString()
#else
clonedString := String.Copy( cString )
#endif
ENDIF
RETURN clonedString
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/space/*" />
FUNCTION Space(dwSize AS DWORD) AS STRING
RETURN STRING{' ',(INT)dwSize}
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/slen/*" />
/// <remarks>
/// The compiler will replace calls to SLen() as much as possible with a direct call to the Length property of a string.
/// </remarks>
FUNCTION SLen(cString AS STRING) AS DWORD
LOCAL len := 0 AS DWORD
IF cString != NULL
len := (DWORD) cString:Length
ENDIF
RETURN len
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/soundex/*" />
FUNCTION SoundEx(cString AS STRING) AS STRING
LOCAL sb AS StringBuilder
LOCAL iLen AS INT
LOCAL ret AS STRING
LOCAL i AS INT
LOCAL cLastChar AS CHAR
LOCAL cSoundExChar AS CHAR
IF String.IsNullOrEmpty(cString)
RETURN "0000"
END IF
cLastChar := (CHAR) 0
cString := cString:ToUpper()
iLen := cString:Length - 1
sb := StringBuilder{}
sb:Append( cString[0] )
FOR i := 1 TO iLen
cSoundExChar := _SoundExChar( cString[i] )
IF cSoundExChar != '0' .AND. cSoundExChar != cLastChar
sb:Append( cSoundExChar )
ENDIF
cLastChar := cSoundExChar
IF sb:Length == 4
EXIT
ENDIF
NEXT
ret := sb:ToString()
RETURN ret:PadRight( 4, '0' )
INTERNAL FUNCTION _SoundExChar( c AS CHAR ) AS CHAR
LOCAL ret AS CHAR
ret := (CHAR) 0
SWITCH c
CASE c'A' ; CASE c'E'; CASE c'I'; CASE c'O'; CASE c'U'; CASE c'Y'
ret := c'0'
CASE c'H' ; CASE c'W'
ret := c'0'
CASE c'B' ; CASE c'F';CASE c'P' ;CASE c'V'
ret := c'1'
CASE c'C' ; CASE c'G';CASE c'J' ;CASE c'K';CASE c'Q';CASE c'S';CASE c'X';CASE c'Z'
ret := c'2'
CASE c'D' ; CASE c'T'
ret := c'3'
CASE c'L'
ret := c'4'
CASE c'M' ; CASE c'N'
ret := c'5'
CASE c'R'
ret := '6'
OTHERWISE
ret := '0'
END SWITCH
RETURN ret
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/stuff/*" />
FUNCTION Stuff(cTarget AS STRING,dwStart AS DWORD,dwDelete AS DWORD,cInsert AS STRING) AS STRING
LOCAL result := cInsert AS STRING
IF cTarget != NULL
IF String.IsNullOrEmpty(cInsert)
cInsert := ""
ENDIF
IF dwStart > 0
dwStart -= 1
ENDIF
LOCAL part1 := cTarget AS STRING
IF (INT) dwStart < cTarget:Length
part1 := cTarget:Substring(0,(INT)dwStart)
ENDIF
LOCAL part2 := "" AS STRING
VAR iOffSet := (INT) (dwStart + dwDelete)
IF iOffSet < cTarget:Length
part2 := cTarget:Substring( iOffSet )
ENDIF
result := part1 + cInsert + part2
ENDIF
RETURN result
/// <exclude />
FUNCTION __SubStr( c AS STRING, nStart AS INT, nLength AS INT ) AS STRING
IF c != NULL
IF nStart == 0
nStart := 1
ENDIF
IF nStart < 0
nStart := c:Length+nStart+1
ENDIF
IF nLength < 0
nLength := c:Length
ENDIF
IF nStart <= c:Length .AND. nStart > 0
nLength := Math.Min( c:Length - nStart + 1, nLength )
c := c:Substring( nStart - 1, nLength )
ELSE
c := ""
ENDIF
ENDIF
RETURN c
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/substr2/*" />
FUNCTION SubStr2(cTarget AS STRING,dwStart AS DWORD) AS STRING
IF cTarget != NULL
cTarget := __SubStr( cTarget, (INT) dwStart, ( cTarget:Length - (INT) dwStart + 1 ) )
ENDIF
RETURN cTarget
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/substr3/*" />
FUNCTION SubStr3(cTarget AS STRING,dwStart AS DWORD,dwLen AS DWORD) AS STRING
IF cTarget != NULL
cTarget := __SubStr( cTarget, (INT) dwStart, (INT) dwLen )
ENDIF
RETURN cTarget
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/trim/*" />
FUNCTION Trim(cString AS STRING) AS STRING
IF ( cString == NULL )
RETURN cString
ENDIF
RETURN cString:TrimEnd(trimChars)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/upper/*" />
FUNCTION Upper(cString AS STRING) AS STRING
IF cString != NULL
cString := cString:ToUpper()
ENDIF
RETURN cString
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/uppera/*" />
FUNCTION UpperA(cString REF STRING) AS STRING
IF cString != NULL
cString := cString:ToUpper()
ENDIF
RETURN cString
/// <summary>This function is not implemented yet</summary>
/// <param name="cLine"></param>
/// <param name="hfOut"></param>
/// <returns>
/// </returns>
FUNCTION UUDecodeLine(cLine AS STRING,hfOut AS IntPtr) AS DWORD
THROW NotImplementedException{}
//RETURN 0
/// <summary>This function is not implemented yet</summary>
/// <param name="c"></param>
/// <returns>
/// </returns>
FUNCTION UUEncFile(c AS STRING) AS STRING
THROW NotImplementedException{}
//RETURN String.Empty
/// <summary>This function is not implemented yet</summary>
/// <param name="c"></param>
/// <returns>
/// </returns>
FUNCTION UUEncLine(c AS STRING) AS STRING
THROW NotImplementedException{}
//RETURN String.Empty
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/isalpha/*" />
FUNCTION IsAlpha(pszString AS STRING) AS LOGIC
LOCAL ret := FALSE AS LOGIC
IF ! String.IsNullOrEmpty( pszString )
ret := Char.IsLetter( pszString, 0 )
ENDIF
RETURN ret
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/isalnum/*" />
FUNCTION IsAlNum(pszString AS STRING) AS LOGIC
LOCAL ret := FALSE AS LOGIC
IF ! String.IsNullOrEmpty( pszString )
ret := Char.IsLetterOrDigit( pszString, 0 )
ENDIF
RETURN ret
/// <summary>Determine if the leftmost character in a string is alphanumeric..</summary>
/// <param name="cSource">The string to examine.</param>
/// <returns>TRUE if the first character is either alphabetic or numeric otherwise FALSE.</returns>
/// <seealso cref='O:XSharp.Core.Functions.IsAlNum'>IsAlNum</seealso>
/// <seealso cref='O:XSharp.Core.Functions.IsAlpha'>IsAlpha</seealso>
FUNCTION IsAlphaNum(cSource AS STRING) AS LOGIC
RETURN IsAlNum(cSource)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/isdigit/*" />
FUNCTION IsDigit(pszString AS STRING) AS LOGIC
LOCAL ret := FALSE AS LOGIC
IF ! String.IsNullOrEmpty( pszString )
ret := Char.IsDigit(pszString, 0 )
ENDIF
RETURN ret
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/isbdigit/*" />
FUNCTION IsBDigit(pszString AS STRING) AS LOGIC
LOCAL ret := FALSE AS LOGIC
IF ! String.IsNullOrEmpty( pszString )