/
sstr.erl
1028 lines (893 loc) · 31.3 KB
/
sstr.erl
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
%%%
%%% CopyrightBegin%
%%%
%%% (2-clause) Simplified BSD license
%%% Copyright (c) 2013, Simon St.Laurent
%%% All rights reserved.
%%%
%%% Redistribution and use in source and binary forms, with or without
%%% modification, are permitted provided that the following conditions are met:
%%%
%%% Redistributions of source code must retain the above copyright notice,
%%% this list of conditions and the following disclaimer.
%%%
%%% Redistributions in binary form must reproduce the above copyright notice,
%%% this list of conditions and the following disclaimer in the documentation
%%% and/or other materials provided with the distribution.
%%%
%%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
%%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
%%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
%%% A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
%%% HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
%%% SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
%%% LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
%%% DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
%%% ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
%%% TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
%%% THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
%%% SUCH DAMAGE.
%%%
%%% %CopyrightEnd%
%%%
%%% Wrapper library for Erlang functions applied to strings
%%%
%% @author Simon St.Laurent <simonstl@simonstl.com> [http://simonstl.com]
%% @copyright 2012 by Simon St.Laurent
%% @version 0.0001
%% @doc A single library collecting string functions from elsewhere in Erlang.
%% The <code>sstr</code> module assembles string-related functions from the following libraries:
%%
%% <ul>
%% <li><code>string</code> of course.</li>
%%
%% <li><code>unicode</code> because <abbr title="Open Telecom Platform">OTP 20</abbr> provides such functionality.</li>
%%
%% <li><code>lists</code>, because (typical) Erlang strings are lists.</li>
%%
%% <li><code>erlang</code> because some of the basics lurk there.</li>
%%
%% <li><p><code>re</code> (<strong>todo</strong>) because regular expressions are a speedy
%% and effective way to manipulate string content.</p></li>
%% </ul>
%%
%% Developers working with normalized text will probably find most of these functions straightforward.
%% However, a few basic techniques for inspecting strings may be useful when Unicode's variations create
%% surprises.
%%
%% For example, the word études can be represented two ways. The first version represents
%% the opening é as a single character:
%% ```
%% 1> Test="étude".
%% "étude"
%% '''
%% You can see what's inside of that string with <code>io:format</code>'s <code>~w</code>
%% control sequence:
%% ```
%% 2> io:format("~w~n",[Test]).
%%[233,116,117,100,101]
%% ok
%% '''
%% That's five characters, starting with character 233 (or E9 in hex).
%%
%% The second version represents the é as a regular e plus an acute accent. (No, this isn't
%% a smart approach for that easily-managed character, but you may encounter this approach
%% used for other characters or in cases where someone decomposed all the Unicode.)
%% ```
%% 3> Test2=[101,769,116,117,100,101].
%% [101,769,116,117,100,101]
%% 4> io:format("~ts~n",[Test2]).
%% étude
%% ok
%% '''
%% In this case, the é is represented by two characters, 101 and 769. That means
%% the list underneath the string is six characters long.
%%
%% These variations may lead to surprises. [Need to point to normalization / denormalization tools.]
%% ```
%% 5> io:format("~ts~n",[unicode:characters_to_list(Test)]).
%% étude
%% ok
%% '''
%%
-module(sstr).
-export([len/1, length/1, concat/1, concat/2, append/1, append/2, nth/2, hd/1]).
-export([chr/2, str/2, substr/2, substr/3, sub_string/2, sub_string/3, tokens/2]).
-export([join/2, words/1, words/2, chars/2, chars/3, copies/2, strip/1, strip/2]).
-export([strip/3, left/2, left/3, right/2, right/3, centre/2, centre/3, reverse/1]).
-export([reverse/2, to_float/1, to_integer/1, to_lower/1, to_upper/1]).
-export([integer_to_list/1, integer_to_list/2, float_to_list/1, fun_to_list/1]).
-export([list_to_atom/1, equal/2, rchr/2, rstr/2, span/2, cspan/2]).
-type chardata() :: unicode:chardata().
-type direction() :: string:direction().
-type grapheme_cluster() :: string:grapheme_cluster().
%% @doc Returns the length of as string.
%% <br/>
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#len-1 string:len/1].
%%
%% @deprecated Please use the {@link length/1} instead.
%% @param String input string
%% @returns length of a data list.
-spec len(String) -> Length when
String:: chardata(),
Length :: non_neg_integer().
len(String) -> string:len(String).
%% @doc Counts the number of characters in a string.
%% <br/>
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#length-1 string:length/1].
%%
%% @param String input string
%% @returns count of characters in a string.
-spec length(String) -> Length when
String:: chardata(),
Length :: non_neg_integer().
length(String) -> string:length(String).
%% @doc Checks whether two strings are equal. This functions is case sensative.
%% <br/>
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#equal-4 string:equal/4].
%%
%% @param String1 input string
%% @param String2 input string
%% @returns Result of comparison unicode normalized character lists.
-spec equal(String1, String2) -> Result when
String1 :: chardata(),
String2 :: chardata(),
Result :: boolean().
equal(String1, String2) -> string:equal(String1, String2, false, nfc).
%% @doc Returs a single string containing all the parts from the ListOfStrings concatenated in the order they appear in the list.
%% <br/>
%% <b>See also:</b>
%% [http://erlang.org/doc/man/lists.html#concat-1 lists:concat/1].
%%
%% @param ListOfStrings list of strings
%% @returns new string
-spec concat(ListOfStrings) -> NewString when
ListOfStrings :: [chardata()],
NewString :: chardata().
concat(ListOfStrings) -> lists:concat(ListOfStrings).
%% @doc Returs a single string containing the two parts from the arguments.
%% <br/>
%% <b>See also:</b>
%% [http://erlang.org/doc/man/unicode.html#characters_to_list-2 unicode:characters_to_list/2].
%%
%% @param String1 input string
%% @param String2 input string
%% @returns new string
-spec concat(String1, String2) -> NewString when
String1 :: chardata(),
String2 :: chardata(),
NewString :: chardata().
concat(String1, String2) -> unicode:characters_to_list([String1, String2], utf8 ).
%% @doc Returs a single string containing all the parts from the arguments in the ListOfStrings.
%% <br/>
%% <b>See also:</b>
%% [http://erlang.org/doc/man/lists.html#append-1 lists:append/1].
%%
%% @param List list of strings
%% @returns new string
-spec append(ListOfStrings) -> NewString when
ListOfStrings :: [chardata()],
NewString :: chardata().
append(ListOfStrings) -> lists:append(ListOfStrings).
%% @doc Returs a single string containing all the parts from the arguments.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:append("abc", "def").
%% "abcdef"
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/lists.html#append-1 lists:append/1].
%%
%% @param String1 input string
%% @param String2 input string
%% @returns new string
-spec append(String1, String2) -> NewString when
String1 :: chardata(),
String2 :: chardata(),
NewString :: chardata().
append(String1, String2) -> lists:append(String1, String2).
%% @doc Returns the character at the specified position.
%% Position start at 0.
%% <br/>
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#length-1 string:length/1],
%% [http://erlang.org/doc/man/string.html#next_grapheme-1 string:next_grapheme/1].
%%
%% @param Position the specified position
%% @param String input string
%% @returns character as a string
-spec nth(Position, String) -> Character when
Position :: non_neg_integer(),
String :: chardata(),
Character :: chardata() | {error, chardata()} | {error, badarg}.
nth(Position, String) ->
Length = string:length(String),
nth(Position, String, Length, 0).
-spec nth(Position, String, Length, Start) ->
chardata() | {error, chardata()} | {error, badarg} when
Position :: non_neg_integer(),
String :: chardata(),
Length :: non_neg_integer(),
Start :: non_neg_integer().
nth(Position, _String, Length, Start) when Length =< Position; Start == Length ->
{error, badarg};
nth(Position, String, Length, Start) ->
[Grapheme|Rest] = string:next_grapheme(String),
nth(Position, Rest, Length, Start, Grapheme).
nth(Position, _String, _Length, Start, Grapheme) when Position == Start ->
unicode:characters_to_list([Grapheme]);
nth(Position, String, Length, Start, _Grapheme) ->
nth(Position,String, Length, Start+1).
%% @doc Returns the first character of the string.
%% <br/>
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#next_grapheme-1 string:next_grapheme/1],
%% [http://erlang.org/doc/man/unicode.html#characters_to_list-1 unicode:characters_to_list/1].
%%
%% @param String input string
%% @returns character as a string
-spec hd(String) -> FirstCharacter when
String :: chardata(),
FirstCharacter :: chardata().
hd(String) ->
[Grapheme|_] = string:next_grapheme(String),
unicode:characters_to_list([Grapheme]).
%% @doc Returns the position where the specified character first appears
%% <br/>
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#find-2 string:find/2].
%%
%% @param String input string
%% @param CharToFind char to find as a string
%% @returns position
%% @see sstr:length/1
-spec chr(String, CharToFind) -> Position when
String :: chardata(),
CharToFind :: chardata(),
Position :: non_neg_integer() | {error, badarg}.
chr(String, CharToFind) ->
Result = string:find(String,CharToFind),
Length = sstr:length(String),
chr(Length, String, Result).
chr(_Length, _String, Result) when Result == nomatch -> {error, badarg};
chr(Length, _String, Result)-> Length - sstr:length(Result).
%% @doc Returns the position where the specified character last appears.
%% <br/>
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#find-2 string:find/2].
%%
%% @param String input string
%% @param CharToFind char to find as a string
%% @returns position
%% @see sstr:length/1
%% @see sstr:chr/3
-spec rchr(String, CharToFind) -> Position when
String :: chardata(),
CharToFind :: chardata(),
Position :: non_neg_integer() | {error, badarg}.
rchr(String, CharToFind) ->
Result = string:find(String,CharToFind, trailing),
Length = sstr:length(String),
chr(Length, String, Result).
%% @doc Returns the position of the first appearance of a substring in a string.
%% @param String input string
%% @param SubstringToFind substring
%% @returns position
%% @see sstr:chr/2
-spec str(String, SubstringToFind) -> Position when
String :: chardata(),
SubstringToFind :: chardata(),
Position :: non_neg_integer() | {error, badarg}.
str(String, SubstringToFind) -> chr(String, SubstringToFind).
%% @doc Returns the position of the last appearance of a substring in a string.
%% @param String input string
%% @param SubstringToFind substring
%% @returns position
%% @see sstr:rchr/2
-spec rstr(String, SubstringToFind) -> Position when
String :: chardata(),
SubstringToFind :: chardata(),
Position :: non_neg_integer() | {error, badarg}.
rstr(String, SubstringToFind) -> rchr(String, SubstringToFind).
%% @doc Returns a segment of a string from a given position to the end.
%% <br/>
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#slice-2 string:slice/2].
%%
%% @param String input string
%% @param Start start position
%% @returns sstr:substring
-spec substr(String, Start) -> SubString when
String :: chardata(),
Start :: non_neg_integer(),
SubString :: chardata().
substr(String, Start) -> string:slice(String, Start).
%% @doc Returns a segment of a string from a given position of a given length.
%% <br/>
%% <b>See also:</b> [http://erlang.org/doc/man/string.html#slice-4 string:slice/4].
%%
%% @param String input string
%% @param Start start position
%% @param Length a length of the segment
%% @returns sstr:substring
-spec substr(String, Start, Length) -> SubString when
String :: chardata(),
Start :: non_neg_integer(),
Length :: non_neg_integer(),
SubString :: chardata().
substr(String, Start, Length) -> string:slice(String, Start, Length).
%% @doc Returns a segment from a string between a Start position and the end.
%% @param String input string
%% @param Start start position
%% @returns substring
%% @see sstr:substr/2
-spec sub_string(String, Start) -> SubString when
String :: chardata(),
Start :: non_neg_integer(),
SubString :: chardata().
sub_string(String, Start) -> substr(String, Start).
%% @doc Returns segment from a string between two positions.
%% @param String input string
%% @param Start start position
%% @param End end position
%% @returns substring
%% @see sstr:substr/3
-spec sub_string(String, Start, End) -> SubString when
String :: chardata(),
Start :: non_neg_integer(),
End :: non_neg_integer(),
SubString :: chardata().
sub_string(String, Start, End) -> substr(String, Start, End-Start).
%% @doc Returns the length of the initial segment of a string that contains only the characters in CharsList.
%% <br/>
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#take-2 string:take/2].
%%
%% @param String input string
%% @param CharsList list of characters
%% @returns length of the segment
%% @see length/1
-spec span(String, CharsList) -> Length when
String :: chardata(),
CharsList :: [grapheme_cluster()],
Length :: non_neg_integer().
span(String, CharsList) ->
{Result,_} = string:take(String, CharsList),
sstr:length(Result).
%% @doc Returns the length of the initial segment of a string that contains only characters NOT in CharsList.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:cspan("\t abcdef", " \t").
%% 0
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#take-2 string:take/3].
%%
%% @param String input string
%% @param CharsList list of characters
%% @returns length of the segment
%% @see sstr:length/1
-spec cspan(String, CharsList) -> Length when
String :: chardata(),
CharsList :: [grapheme_cluster()],
Length :: non_neg_integer().
cspan(String, CharsList) ->
{Result,_} = string:take(String, CharsList, true),
sstr:length(Result).
%% @doc Returns a list of tokens extracted from the string, using the list of characters in SeparatorChars to decompose the string.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:tokens("abc defxxghix jkl", "x ").
%% ["abc", "def", "ghi", "jkl"]
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#lexemes-2 string:lexemes/2].
%%
%% @param String input string
%% @param SeparatorChars string separator
%% @returns list of tokens
-spec tokens(String, SeparatorChars) -> List when
String :: chardata(),
SeparatorChars :: [grapheme_cluster()],
List :: [chardata()].
tokens(String, SeparatorChars) -> string:lexemes(String, SeparatorChars).
%% @doc Returns a string made from the list of pieces with specified separators added.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:join(["one", "two", "three"], ", ").
%% "one, two, three"
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/lists.html#join-2 lists:join/2].
%% @param List the list of pieces
%% @param Separator separator of pieces
%% @returns new string
%% @see sstr:concat/1
-spec join(List, Separator) -> String when
List :: [chardata()],
Separator :: chardata(),
String :: chardata().
join(ListOfStrings, Separator) ->
List = lists:join(Separator, ListOfStrings),
sstr:concat(List).
%% @doc Returns the number of words in the string, where blanks are used to indicate breaks between words.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:words("Hello old boy!").
%% 3
%% '''
%% @param String input string
%% @returns the number of words in the string
%% @see words/2
-spec words(String) -> Count when
String :: chardata(),
Count :: non_neg_integer().
words(String) -> sstr:words(String, " " ++ [$\r,$\n]).
%% @doc Returns the number of words in the string, where WordSeparator is used to indicate breaks between words.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:words(" Hello old boy!", [$o]).
%% 4
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#lexemes-2 string:lexemes/2],
%% [http://erlang.org/doc/man/erlang.html#length-1 erlang:length/1].
%%
%% @param String input string
%% @param WordSeparator separator
%% @returns the number of words in the string
-spec words(String, WordSeparator) -> Count when
String :: chardata(),
WordSeparator :: grapheme_cluster(),
Count :: non_neg_integer().
words(String, WordSeparator) ->
List = string:lexemes(String, WordSeparator),
erlang:length(List).
%% @doc Returns a string that repeats the given character RepeatCount number of times.
%%
%% <b>See also:</b>
%% [http://erlang.org/doc/man/lists.html#duplicate-2 lists:duplicate/2].
%%
%% @param Character a character
%% @param RepeatCount the number of times
%% @returns new string
%% @see concat/1
-spec chars(Character, RepeatCount) -> String when
Character :: grapheme_cluster(),
RepeatCount :: non_neg_integer(),
String :: chardata().
chars(Character, RepeatCount) ->
List = lists:duplicate(RepeatCount, Character),
sstr:concat(List).
%% @doc Returns a string that repeats the given character RepeatCount number of times and appends a Tail.
%%
%% <b>See also:</b>
%% [http://erlang.org/doc/man/lists.html#duplicate-2 lists:duplicate/2].
%%
%% @param Character a character
%% @param RepeatCount the number of times
%% @param Tail additional string
%% @returns new string
%% @see concat/2
-spec chars(Character, RepeatCount, Tail) -> String when
Character :: grapheme_cluster(),
RepeatCount :: non_neg_integer(),
Tail :: chardata(),
String :: chardata().
chars(Character, RepeatCount, Tail) ->
String = sstr:chars(Character, RepeatCount),
sstr:concat(String, Tail).
%% @doc Returns a string that repeats the given string RepeatCount number of times.
%% @equiv chars/2
%% @param String input string
%% @param RepeatCount the number of times
%% @returns new string
%% @see sstr:chars/2
-spec copies(String, RepeatCount) -> NewString when
String :: chardata(),
RepeatCount :: non_neg_integer(),
NewString :: chardata().
copies(String, RepeatCount) -> sstr:chars(String, RepeatCount).
%% @doc Returns a string with both leading and trailing whitespace removed
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> string:strip("\r Hello \n").
%% "Hello"
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#trim-1 string:trim/1].
%%
%% @param String input string
%% @returns new string
-spec strip(String) -> NewString when
String :: chardata(),
NewString :: chardata().
strip(String) -> string:trim(String).
%% @doc Returns a string with leading and/or trailing whitespace removed.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:strip("Hello \n", leading).
%% "Hello \n"
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#trim-2 string:trim/2].
%%
%% @param String input string
%% @param Direction leading | trailing | both
%% @returns new string
-spec strip(String, Direction) -> NewString when
String :: chardata(),
Direction :: direction() | both,
NewString :: chardata().
strip(String, Direction) -> string:trim(String, Direction).
%% @doc Returns a string with leading and/or trailing specified characters removed.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:strip(<<".Hello.\n">>, trailing, "\n.").
%% <<".Hello">>
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#trim-3 string:trim/3].
%%
%% @param String input string
%% @param Direction leading | trailing | both
%% @returns new string
-spec strip(String, Direction, Characters) -> NewString when
String :: chardata(),
Direction :: direction() | both,
Characters :: [string:grapheme_cluster()],
NewString :: chardata().
strip(String, Direction, Characters) -> string:trim(String, Direction, Characters).
%% @doc Returns a string of a specified length, padded with spaces on the left if needed.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:left("Hello", 10).
%% "Hello "
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#pad-4 string:pad/4],
%% [http://erlang.org/doc/man/lists.html#flatten-1 lists:flatten/1].
%%
%% @param String input string
%% @param TargetLength length of a new string
%% @returns new string
-spec left(String, TargetLength) -> NewString when
String :: chardata(),
TargetLength :: non_neg_integer(),
NewString :: chardata().
left(String, TargetLength) ->
List = string:pad(String, TargetLength, leading, " "),
lists:flatten(List).
%% @doc Returns a string of a specified length, padded with PaddingCharacter on the left if needed.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:left("Hello", 10, $.).
%% ".....Hello"
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#pad-4 string:pad/4],
%% [http://erlang.org/doc/man/lists.html#flatten-1 lists:flatten/1].
%%
%% @param String input string
%% @param TargetLength length of a new string
%% @returns new string
-spec left(String, TargetLength, PaddingCharacter) -> NewString when
String :: chardata(),
TargetLength :: non_neg_integer(),
PaddingCharacter :: string:grapheme_cluster(),
NewString :: chardata().
left(String, TargetLength, PaddingCharacter) ->
List = string:pad(String, TargetLength, leading, PaddingCharacter),
lists:flatten(List).
%% @doc Returns a string of a specified length, padded with spaces on the right if needed.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:right("Hello", 10).
%% "Hello "
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#pad-4 string:pad/4],
%% [http://erlang.org/doc/man/lists.html#flatten-1 lists:flatten/1].
%%
%% @param String input string
%% @param TargetLength length of a new string
%% @returns new string
-spec right(String, TargetLength) -> NewString when
String :: chardata(),
TargetLength :: non_neg_integer(),
NewString :: chardata().
right(String, TargetLength) ->
List = string:pad(String, TargetLength, trailing, " "),
lists:flatten(List).
%% @doc Returns a string of a specified length, padded with PaddingCharacter on the right if needed.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:left("Hello", 10, $.).
%% "Hello....."
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#pad-4 string:pad/4],
%% [http://erlang.org/doc/man/lists.html#flatten-1 lists:flatten/1].
%%
%% @param String input string
%% @param TargetLength length of a new string
%% @returns new string
-spec right(String, TargetLength, PaddingCharacter) -> NewString when
String :: chardata(),
TargetLength :: non_neg_integer(),
PaddingCharacter :: string:grapheme_cluster(),
NewString :: chardata().
right(String, TargetLength, PaddingCharacter) ->
List = string:pad(String, TargetLength, trailing, PaddingCharacter),
lists:flatten(List).
%% @doc Returns a string of a specified length, padded with spaces on the left and right if needed.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:centre("Hello", 10).
%% " Hello "
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#pad-4 string:pad/4],
%% [http://erlang.org/doc/man/lists.html#flatten-1 lists:flatten/1].
%%
%% @param String input string
%% @param TargetLength length of a new string
%% @returns new string
-spec centre(String, TargetLength) -> NewString when
String :: chardata(),
TargetLength :: non_neg_integer(),
NewString :: chardata().
centre(String, TargetLength) ->
List = string:pad(String, TargetLength, both, " "),
lists:flatten(List).
%% @doc Returns a string of a specified length, padded with PaddingCharacter on the left and right if needed.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:centre("Hello", 10, $.).
%% ".....Hello....."
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#pad-4 string:pad/4],
%% [http://erlang.org/doc/man/lists.html#flatten-1 lists:flatten/1].
%% @param String input string
%% @param TargetLength length of a new string
%% @returns new string
-spec centre(String, TargetLength, PaddingCharacter) -> NewString when
String :: chardata(),
TargetLength :: non_neg_integer(),
PaddingCharacter :: string:grapheme_cluster(),
NewString :: chardata().
centre(String, TargetLength, PaddingCharacter) ->
List = string:pad(String, TargetLength, both, PaddingCharacter),
lists:flatten(List).
%% @doc Returns a string which is in reverse order.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> Reverse = sstr:reverse("ÅÄÖ").
%% [79,776,65,776,65,778]
%% 2> io:format("~ts~n",[Reverse]).
%% ÖÄÅ
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#reverse-1 string:reverse/1],
%% [http://erlang.org/doc/man/lists.html#flatten-1 lists:flatten/1].
%%
%% @param String input string
%% @returns reversed input string
-spec reverse(String) -> NewString when
String :: chardata(),
NewString :: chardata().
reverse(String) ->
ReversedString = string:reverse(unicode:characters_to_nfd_binary(String)),
lists:flatten(ReversedString).
%% @doc Returns a string which is in reverse order, with a tail added to the end.
%% @param String input string
%% @param Tail additional string
%% @returns new string
% @see sstr:reverse/1
% @see sstr:concat/2
-spec reverse(String, Tail) -> NewString when
String :: chardata(),
Tail :: chardata(),
NewString :: chardata().
reverse(String, Tail) ->
ReversedString = sstr:reverse(String),
sstr:concat(ReversedString, Tail).
%% @doc Converts a string to a float representing its contents.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> F1 = sstr:to_float("1.0-1.0e-1"),
%% 1> F2 = sstr:to_float("-1.0e-1"),
%% 1> F1+F2.
%% 0.9
%% 2> sstr:to_float("3/2=1.5").
%% {error,no_float}
%% 3> sstr:to_float("-1.5eX").
%% -1.5
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#to_float-1 string:to_float/1].
%%
%% @param String input string
%% @returns a float value
-spec to_float(String) -> Float | {error, Reason} when
String :: chardata(),
Float :: float(),
Reason :: no_float | badarg.
to_float(String) ->
case string:to_float(String) of
{error, Reason} -> {error, Reason};
{Float, _Rest} -> Float
end.
%% @doc Converts a string to an integer representing its contents.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> I1 = sstr:to_integer("33"),
%% 1> I2 = sstr:to_integer("+22"),
%% 1> I1-I2.
%% 11
%% 2> sstr:to_integer("0.5").
%% 0
%% 3> sstr:to_integer("x=2").
%% {error,no_integer}
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#to_integer-1 string:to_integer/1].
%%
%% @param String input string
%% @returns an integer value
-spec to_integer(String) -> Int | {error, Reason} when
String :: chardata(),
Int :: integer(),
Reason :: no_integer | badarg.
to_integer(String) ->
case string:to_integer(String) of
{error, Reason} -> {error, Reason};
{Int, _Rest} -> Int
end.
%% @doc Returns an lower-case version of a string.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:to_lower("MICHAŁ").
%% "michał"
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#lowercase-1 string:lowercase/1].
%%
%% @param String input string
%% @returns lower case string
-spec to_lower(String) -> NewString when
String :: chardata(),
NewString :: chardata() | {error, chardata()}.
to_lower(String) -> string:lowercase(String).
%% @doc Returns an upper-case version of a string.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:to_upper("michał").
%% "MICHAŁ"
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/string.html#uppercase-1 string:uppercase/1].
%%
%% @param String input string
%% @returns upper case string
-spec to_upper(String) -> NewString when
String :: chardata(),
NewString :: chardata() | {error, chardata()}.
to_upper(String) -> string:uppercase(String).
%% @doc Converts an integer to a string using Base 10 representation.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:integer_to_list(77).
%% "77"
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/erlang.html#integer_to_list-1 erlang:integer_to_list/1].
%%
%% @param Int integer value
%% @returns string
-spec integer_to_list(Int) -> String when
Int :: integer(),
String :: string().
integer_to_list(Integer) -> erlang:integer_to_list(Integer).
%% @doc Converts an integer to a string representing it in the specified Base.
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:integer_to_list(36,36).
%% "10"
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/erlang.html#integer_to_list-2 erlang:integer_to_list/2].
%%
%% @param Int integer value
%% @returns string
-spec integer_to_list(Int, Base) -> String when
Int :: integer(),
Base :: [2..36],
String :: string().
integer_to_list(Integer, Base) -> erlang:integer_to_list(Integer, Base).
%% @doc Converts a float to a string
%% <br/>
%% <b>Example:</b>
%% ```
%% 1> sstr:float_to_list(1.0).
%% "1.0"
%% '''
%% <b>See also:</b>
%% [http://erlang.org/doc/man/erlang.html#float_to_list-1 erlang:float_to_list/1],
%% [http://erlang.org/doc/man/erlang.html#float_to_list-2 erlang:float_to_list/2].
%%
%% @param Float float value
%% @returns string
-spec float_to_list(Float) -> String when
Float :: float(),
String :: string().
float_to_list(Float) ->
try
erlang:float_to_list(Float,[{decimals,253},compact])
catch
error:badarg ->
float_to_list_scientific(Float,4)
end.
%% @doc Converts a float to a string using scientific option.
%% @private
%% @param Float float value
%% @param ScientificDecimals scientific decimal value
%% @returns string that represents float in scientific way.
%%
%% This function used for beautifying scientific string
float_to_list_scientific(Float, 0) -> erlang:float_to_list(Float,[{scientific, 0}]);
float_to_list_scientific(Float, ScientificDecimals) ->
Result = erlang:float_to_list(Float,[{scientific, ScientificDecimals}]),
case sstr:str(Result,"00e") of
{error, badarg} ->
Result;
_ -> float_to_list_scientific(Float,ScientificDecimals-1)