Skip to content

Commit e0f1497

Browse files
committed
stdlib: Update string to use doctests for its examples
1 parent 8764282 commit e0f1497

2 files changed

Lines changed: 32 additions & 114 deletions

File tree

lib/stdlib/src/string.erl

Lines changed: 21 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ _Example:_
355355
```erlang
356356
1> Reverse = string:reverse(unicode:characters_to_nfd_binary("ÅÄÖ")).
357357
[[79,776],[65,776],[65,778]]
358-
2> io:format("~ts~n",[Reverse]).
359-
ÖÄÅ
358+
2> io:format("%% ~ts~n",[Reverse]).
359+
%% ÖÄÅ
360360
```
361361
""".
362362
-doc(#{group => <<"Functions">>,since => <<"OTP 20.0">>}).
@@ -445,10 +445,10 @@ _Example:_
445445
```erlang
446446
1> string:pad(<<"He̊llö"/utf8>>, 8).
447447
[<<72,101,204,138,108,108,195,182>>,32,32,32]
448-
2> io:format("'~ts'~n",[string:pad("He̊llö", 8, leading)]).
449-
' He̊llö'
450-
3> io:format("'~ts'~n",[string:pad("He̊llö", 8, both)]).
451-
' He̊llö '
448+
2> io:format("%% '~ts'~n",[string:pad("He̊llö", 8, leading)]).
449+
%% ' He̊llö'
450+
3> io:format("%% '~ts'~n",[string:pad("He̊llö", 8, both)]).
451+
%% ' He̊llö '
452452
```
453453
""".
454454
-doc(#{group => <<"Functions">>,since => <<"OTP 20.0">>}).
@@ -543,9 +543,9 @@ Returns a string where any trailing `\n` or `\r\n` have been removed from
543543
_Example:_
544544

545545
```erlang
546-
182> string:chomp(<<"\nHello\n\n">>).
546+
1> string:chomp(<<"\nHello\n\n">>).
547547
<<"\nHello">>
548-
183> string:chomp("\nHello\r\r\n").
548+
2> string:chomp("\nHello\r\r\n").
549549
"\nHello\r"
550550
```
551551
""".
@@ -583,13 +583,13 @@ taken.
583583
_Example:_
584584

585585
```erlang
586-
5> string:take("abc0z123", lists:seq($a,$z)).
586+
1> string:take("abc0z123", lists:seq($a,$z)).
587587
{"abc","0z123"}
588-
6> string:take(<<"abc0z123">>, lists:seq($0,$9), true, leading).
588+
2> string:take(<<"abc0z123">>, lists:seq($0,$9), true, leading).
589589
{<<"abc">>,<<"0z123">>}
590-
7> string:take("abc0z123", lists:seq($0,$9), false, trailing).
590+
3> string:take("abc0z123", lists:seq($0,$9), false, trailing).
591591
{"abc0z","123"}
592-
8> string:take(<<"abc0z123">>, lists:seq($a,$z), true, trailing).
592+
4> string:take(<<"abc0z123">>, lists:seq($a,$z), true, trailing).
593593
{<<"abc0z">>,<<"123">>}
594594
```
595595
""".
@@ -661,7 +661,7 @@ tested for equality.
661661
_Example:_
662662

663663
```erlang
664-
2> string:lowercase(string:uppercase("Michał")).
664+
1> string:lowercase(string:uppercase("Michał")).
665665
"michał"
666666
```
667667
""".
@@ -746,8 +746,8 @@ _Example:_
746746

747747
```erlang
748748
1> {I1,Is} = string:to_integer("33+22"),
749-
1> {I2,[]} = string:to_integer(Is),
750-
1> I1-I2.
749+
{I2,[]} = string:to_integer(Is),
750+
I1-I2.
751751
11
752752
2> string:to_integer("0.5").
753753
{0,".5"}
@@ -787,8 +787,8 @@ _Example:_
787787

788788
```erlang
789789
1> {F1,Fs} = string:to_float("1.0-1.0e-1"),
790-
1> {F2,[]} = string:to_float(Fs),
791-
1> F1+F2.
790+
{F2,[]} = string:to_float(Fs),
791+
F1+F2.
792792
0.9
793793
2> string:to_float("3/2=1.5").
794794
{error,no_float}
@@ -871,11 +871,11 @@ parts. `Where`, default `leading`, indicates whether the `leading`, the
871871
_Example:_
872872

873873
```erlang
874-
0> string:split("ab..bc..cd", "..").
874+
1> string:split("ab..bc..cd", "..").
875875
["ab","bc..cd"]
876-
1> string:split(<<"ab..bc..cd">>, "..", trailing).
876+
2> string:split(<<"ab..bc..cd">>, "..", trailing).
877877
[<<"ab..bc">>,<<"cd">>]
878-
2> string:split(<<"ab..bc....cd">>, "..", all).
878+
3> string:split(<<"ab..bc....cd">>, "..", all).
879879
[<<"ab">>,<<"bc">>,<<>>,<<"cd">>]
880880
```
881881
""".
@@ -2507,13 +2507,6 @@ Returns the position where the first occurrence of `SubString` begins in
25072507
`String`. Returns `0` if `SubString` does not exist in `String`.
25082508

25092509
This function is [obsolete](`m:string#obsolete-api-functions`). Use `find/2`.
2510-
2511-
_Example:_
2512-
2513-
```erlang
2514-
1> string:str(" Hello Hello World World ", "Hello World").
2515-
8
2516-
```
25172510
""".
25182511
-doc(#{group => <<"Obsolete API functions">>}).
25192512
-spec str(String, SubString) -> Index when
@@ -2536,13 +2529,6 @@ Returns the position where the last occurrence of `SubString` begins in
25362529
`String`. Returns `0` if `SubString` does not exist in `String`.
25372530

25382531
This function is [obsolete](`m:string#obsolete-api-functions`). Use `find/3`.
2539-
2540-
_Example:_
2541-
2542-
```erlang
2543-
1> string:rstr(" Hello Hello World World ", "Hello World").
2544-
8
2545-
```
25462532
""".
25472533
-doc(#{group => <<"Obsolete API functions">>}).
25482534
-spec rstr(String, SubString) -> Index when
@@ -2572,13 +2558,6 @@ Returns the length of the maximum initial segment of `String`, which consists
25722558
entirely of characters from `Chars`.
25732559

25742560
This function is [obsolete](`m:string#obsolete-api-functions`). Use `take/2`.
2575-
2576-
_Example:_
2577-
2578-
```erlang
2579-
1> string:span("\t abcdef", " \t").
2580-
5
2581-
```
25822561
""".
25832562
-doc(#{group => <<"Obsolete API functions">>}).
25842563
-spec span(String, Chars) -> Length when
@@ -2600,13 +2579,6 @@ Returns the length of the maximum initial segment of `String`, which consists
26002579
entirely of characters not from `Chars`.
26012580

26022581
This function is [obsolete](`m:string#obsolete-api-functions`). Use `take/3`.
2603-
2604-
_Example:_
2605-
2606-
```erlang
2607-
1> string:cspan("\t abcdef", " \t").
2608-
0
2609-
```
26102582
""".
26112583
-doc(#{group => <<"Obsolete API functions">>}).
26122584
-spec cspan(String, Chars) -> Length when
@@ -2644,13 +2616,6 @@ Returns a substring of `String`, starting at position `Start`, and ending at the
26442616
end of the string or at length `Length`.
26452617

26462618
This function is [obsolete](`m:string#obsolete-api-functions`). Use `slice/3`.
2647-
2648-
_Example:_
2649-
2650-
```erlang
2651-
1> substr("Hello World", 4, 5).
2652-
"lo Wo"
2653-
```
26542619
""".
26552620
-doc(#{group => <<"Obsolete API functions">>}).
26562621
-spec substr(String, Start, Length) -> SubString when
@@ -2675,14 +2640,7 @@ substr2([_|String], S) -> substr2(String, S-1).
26752640
Returns a list of tokens in `String`, separated by the characters in
26762641
`SeparatorList`.
26772642

2678-
_Example:_
2679-
2680-
```erlang
2681-
1> tokens("abc defxxghix jkl", "x ").
2682-
["abc", "def", "ghi", "jkl"]
2683-
```
2684-
2685-
Notice that, as shown in this example, two or more adjacent separator characters
2643+
Notice that two or more adjacent separator characters
26862644
in `String` are treated as one. That is, there are no empty strings in the
26872645
resulting list of tokens.
26882646

@@ -2803,13 +2761,6 @@ words(String) -> words(String, $\s).
28032761
Returns the number of words in `String`, separated by blanks or `Character`.
28042762

28052763
This function is [obsolete](`m:string#obsolete-api-functions`). Use `lexemes/2`.
2806-
2807-
_Example:_
2808-
2809-
```erlang
2810-
1> words(" Hello old boy!", $o).
2811-
4
2812-
```
28132764
""".
28142765
-doc(#{group => <<"Obsolete API functions">>}).
28152766
-spec words(String, Character) -> Count when
@@ -2841,13 +2792,6 @@ or `Character`s.
28412792

28422793
This function is [obsolete](`m:string#obsolete-api-functions`). Use
28432794
`nth_lexeme/3`.
2844-
2845-
_Example:_
2846-
2847-
```erlang
2848-
1> string:sub_word(" Hello old boy !",3,$o).
2849-
"ld b"
2850-
```
28512795
""".
28522796
-doc(#{group => <<"Obsolete API functions">>}).
28532797
-spec sub_word(String, Number, Character) -> Word when
@@ -2901,13 +2845,6 @@ Returns a string, where leading or trailing, or both, blanks or a number of
29012845
[`strip/1`](`strip/1`) is equivalent to [`strip(String, both)`](`strip/2`).
29022846

29032847
This function is [obsolete](`m:string#obsolete-api-functions`). Use `trim/3`.
2904-
2905-
_Example:_
2906-
2907-
```erlang
2908-
1> string:strip("...Hello.....", both, $.).
2909-
"Hello"
2910-
```
29112848
""".
29122849
-doc(#{group => <<"Obsolete API functions">>}).
29132850
-spec strip(String, Direction, Character) -> Stripped when
@@ -2954,13 +2891,6 @@ padded with blanks or `Character`s.
29542891

29552892
This function is [obsolete](`m:string#obsolete-api-functions`). Use `pad/2` or
29562893
`pad/3`.
2957-
2958-
_Example:_
2959-
2960-
```erlang
2961-
1> string:left("Hello",10,$.).
2962-
"Hello....."
2963-
```
29642894
""".
29652895
-doc(#{group => <<"Obsolete API functions">>}).
29662896
-spec left(String, Number, Character) -> Left when
@@ -2996,13 +2926,6 @@ margin is fixed. If the length of `(String)` < `Number`, then `String` is padded
29962926
with blanks or `Character`s.
29972927

29982928
This function is [obsolete](`m:string#obsolete-api-functions`). Use `pad/3`.
2999-
3000-
_Example:_
3001-
3002-
```erlang
3003-
1> string:right("Hello", 10, $.).
3004-
".....Hello"
3005-
```
30062929
""".
30072930
-doc(#{group => <<"Obsolete API functions">>}).
30082931
-spec right(String, Number, Character) -> Right when
@@ -3073,13 +2996,6 @@ Returns a substring of `String`, starting at position `Start` to the end of the
30732996
string, or to and including position `Stop`.
30742997

30752998
This function is [obsolete](`m:string#obsolete-api-functions`). Use `slice/3`.
3076-
3077-
_Example:_
3078-
3079-
```erlang
3080-
1> sub_string("Hello World", 4, 8).
3081-
"lo Wo"
3082-
```
30832999
""".
30843000
-doc(#{group => <<"Obsolete API functions">>}).
30853001
-spec sub_string(String, Start, Stop) -> SubString when
@@ -3161,13 +3077,6 @@ Returns a string with the elements of `StringList` separated by the string in
31613077

31623078
This function is [obsolete](`m:string#obsolete-api-functions`). Use
31633079
`lists:join/2`.
3164-
3165-
_Example:_
3166-
3167-
```erlang
3168-
1> join(["one", "two", "three"], ", ").
3169-
"one, two, three"
3170-
```
31713080
""".
31723081
-doc(#{group => <<"Obsolete API functions">>}).
31733082
-spec join(StringList, Separator) -> String when

lib/stdlib/test/string_SUITE.erl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
prefix/1, split/1, replace/1, find/1,
4242
lexemes/1, nth_lexeme/1, cd_gc/1,
4343
jaro_similarity/1,
44-
meas/1
44+
meas/1,
45+
doctests/1
4546
]).
4647

4748
-export([len/1,old_equal/1,old_concat/1,chr_rchr/1,str_rstr/1]).
@@ -60,7 +61,7 @@ suite() ->
6061
{timetrap,{minutes,1}}].
6162

6263
all() ->
63-
[{group, chardata}, {group, list_string}].
64+
[{group, chardata}, {group, list_string}, doctests].
6465

6566
groups() ->
6667
[{chardata,
@@ -1659,3 +1660,11 @@ join(Config) when is_list(Config) ->
16591660
%% invalid arg type
16601661
?assertError(_, string:join([apa], "")),
16611662
ok.
1663+
1664+
-include_lib("kernel/include/eep48.hrl").
1665+
1666+
doctests(_Config) ->
1667+
{ok, #docs_v1{ docs = Docs }} = code:get_doc(string),
1668+
ObsoleteFunctions = [{F,A} || {{function,F,A},_,_,#{},#{group := ~"Obsolete API functions"}} <- Docs],
1669+
ok = ct_doctest:module(string, [{skipped_blocks, 1},
1670+
{missing_tests, [{trim, 2}] ++ ObsoleteFunctions}]).

0 commit comments

Comments
 (0)