Skip to content

Commit a580cc2

Browse files
author
duchier
committed
A new module with convenient operations on strings.
git-svn-id: https://gforge.info.ucl.ac.be/svn/mozart@15962 ada56829-ad1f-0410-b00f-83cda6628aec
1 parent 043b124 commit a580cc2

File tree

3 files changed

+259
-1
lines changed

3 files changed

+259
-1
lines changed

String.oz

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
functor
2+
export
3+
Make Length
4+
ToInt ToAtom ToFloat
5+
Capitalize
6+
Split SplitAtMost Join
7+
ToLower ToUpper
8+
Lstrip Rstrip Strip
9+
Replace ReplaceAtMost
10+
prepare
11+
CharToUpper = Char.toUpper
12+
CharToLower = Char.toLower
13+
CharIsSpace = Char.isSpace
14+
DropWhile = List.dropWhile
15+
16+
Make = VirtualString.toString
17+
18+
ToInt = StringToInt
19+
ToAtom = StringToAtom
20+
ToFloat = StringToFloat
21+
22+
fun {Capitalize S}
23+
case S
24+
of H|T then {CharToUpper H}|T
25+
else S end
26+
end
27+
28+
fun {SplitStart S Next Max}
29+
if S==nil then nil
30+
elseif Max==0 then [S]
31+
else Prefix Suffix in
32+
{Next S Prefix Suffix}
33+
Prefix|if Suffix==unit then nil else
34+
{SplitMore Suffix Next Max-1}
35+
end
36+
end
37+
end
38+
39+
fun {SplitMore S Next Max}
40+
if Max==0 orelse S==nil then [S]
41+
else Prefix Suffix in
42+
{Next S Prefix Suffix}
43+
Prefix|if Suffix==unit then nil else
44+
{SplitMore Suffix Next Max-1}
45+
end
46+
end
47+
end
48+
49+
fun {Split S Sep} {SplitStart S {NextSplitter Sep} ~1} end
50+
fun {SplitAtMost S Sep Max} {SplitStart S {NextSplitter Sep} Max} end
51+
52+
proc {NextSplitWS S Prefix Suffix}
53+
case S
54+
of nil then Prefix=nil Suffix=unit
55+
[] H|T then
56+
if {CharIsSpace H} then
57+
Prefix=nil {DropWhile T CharIsSpace Suffix}
58+
else Prefix2 in
59+
Prefix=(H|Prefix2)
60+
{NextSplitWS T Prefix2 Suffix}
61+
end
62+
end
63+
end
64+
65+
fun {WithPrefix SEP S}
66+
case SEP
67+
of nil then S
68+
[] H|SEP then
69+
case S
70+
of !H|S then {WithPrefix SEP S}
71+
else unit end
72+
end
73+
end
74+
75+
proc {NextSplitSEP S SEP Prefix Suffix}
76+
case S
77+
of nil then Prefix=nil Suffix=unit
78+
elsecase {WithPrefix SEP S}
79+
of unit then
80+
case S
81+
of H|T then Prefix2 in
82+
Prefix=(H|Prefix2)
83+
{NextSplitSEP T SEP Prefix2 Suffix}
84+
end
85+
[] S then Prefix=nil Suffix=S
86+
end
87+
end
88+
89+
proc {NextSplitNULL S Prefix Suffix}
90+
case S
91+
of nil then Prefix=nil Suffix=unit
92+
[] H|T then Prefix=[H] Suffix=T
93+
end
94+
end
95+
96+
fun {NextSplitter Sep}
97+
case Sep
98+
of unit then NextSplitWS
99+
[] nil then NextSplitNULL
100+
else
101+
proc {$ S Prefix Suffix}
102+
{NextSplitSEP S Sep Prefix Suffix}
103+
end
104+
end
105+
end
106+
107+
fun {Join L Sep}
108+
if L==nil then nil else
109+
{FoldR L
110+
fun {$ S Accu}
111+
if Accu==unit then S else
112+
{Append S {Append Sep Accu}}
113+
end
114+
end unit}
115+
end
116+
end
117+
118+
fun {ToUpper S} {Map S CharToUpper} end
119+
fun {ToLower S} {Map S CharToLower} end
120+
121+
fun {Lstrip S Chars}
122+
{DropWhile S
123+
if Chars==unit then CharIsSpace else
124+
fun {$ C} {Member C Chars} end
125+
end}
126+
end
127+
128+
fun {Rstrip S Chars}
129+
{Reverse {Lstrip {Reverse S} Chars}}
130+
end
131+
132+
fun {Strip S Chars}
133+
{Rstrip {Lstrip S Chars} Chars}
134+
end
135+
136+
fun {Replace S Old New}
137+
{Join {Split S Old} New}
138+
end
139+
140+
fun {ReplaceAtMost S Old New Max}
141+
{Join {SplitAtMost S Old Max} New}
142+
end
143+
end

index.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ <H1>Mozart Standard Library</H1>
6161
<LI><A href="wp/index.html">GUI programming</A>
6262
<LI><A href="xml/index.html">XML Support</A>
6363
</UL>
64-
64+
and the following general modules:
65+
<UL>
66+
<LI><A href="string.html"><CODE>x.oz://system/String.ozf</CODE>
67+
</UL>
6568
<HR>
6669
</BODY>
6770
</HTML>

string.html

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2+
<HTML>
3+
<HEAD>
4+
<!-- EDIT -->
5+
<TITLE>Mozart Standard Library: String Support</TITLE>
6+
<STYLE>
7+
BODY {
8+
background-color: white;
9+
margin-left : 2cm;
10+
margin-right : 2cm;
11+
font-family : tahoma,arial,helvetica,sans-serif;
12+
}
13+
H1 {
14+
text-align : center;
15+
color : #9B0000;
16+
}
17+
H2 { color : #FF9933; }
18+
H4 { color : slateblue; }
19+
H3 { color : #881155; }
20+
H5 { color : darkslateblue; }
21+
CODE { color : #663366; }
22+
CODE,TT {
23+
font-family : "lucida console",courier,monospace;
24+
}
25+
CODE.DISPLAY {
26+
display : block;
27+
white-space : pre;
28+
margin-left : 2cm;
29+
margin-top : 1em;
30+
margin-bottom : 1em;
31+
}
32+
P.AUTHOR {
33+
text-align : center;
34+
font-weight : bold;
35+
}
36+
SPAN.MODULE {
37+
color : steelblue;
38+
}
39+
A { color : steelblue; }
40+
SPAN.COMMENT { color: #B22222; }
41+
SPAN.KEYWORD { color: #A020F0; }
42+
SPAN.STRING { color: #BC8F8F; }
43+
SPAN.FUNCTIONNAME { color: #0000FF; }
44+
SPAN.TYPE { color: #228B22; }
45+
SPAN.VARIABLENAME { color: #B8860B; }
46+
SPAN.REFERENCE { color: #5F9EA0; }
47+
SPAN.BUILTIN { color: #DA70D6; }
48+
</STYLE>
49+
</HEAD>
50+
<BODY>
51+
<!-- EDIT -->
52+
<H1>String Support</H1>
53+
<P CLASS="AUTHOR">Denys Duchier</P>
54+
<DL>
55+
<DT><B>module</B>
56+
<DD><SPAN CLASS="MODULE">x-oz://system/String.ozf</SPAN>
57+
</DL>
58+
<HR>
59+
60+
<P>This module provides additional convenience operation on strings.
61+
It should not be confused, with the base <CODE>String</CODE> which is
62+
always present and does not need to be imported.
63+
64+
<H2>Exports</H2>
65+
66+
<DL>
67+
<DT><CODE>{String.make +VS ?S}</CODE>
68+
<DD>turns a virtual string into a plain old regular string
69+
<DT><CODE>{String.length +S ?N}</CODE>
70+
<DD>returns the length <CODE>N</CODE> of string <CODE>S</CODE>
71+
<DT><CODE>{String.toInt +S ?I}</CODE>
72+
<DD>takes a string <CODE>S</CODE> and returns the integer <CODE>I</CODE> of
73+
which it is the textual representation
74+
<DT><CODE>{String.toFloat +S ?F}</CODE>
75+
<DD>takes a string <CODE>S</CODE> and returns the float <CODE>F</CODE> of
76+
which it is the textual representation
77+
<DT><CODE>{String.toInt +S ?A}</CODE>
78+
<DD>takes a string <CODE>S</CODE> and returns the corresponding
79+
atom <CODE>A</CODE>
80+
<DT><CODE>{String.capitalize +S1 ?S2}</CODE>
81+
<DD>takes a string <CODE>S1</CODE> and returns a string <CODE>S2</CODE>
82+
which is identical except for the first letter which has been capitalized
83+
<DT><CODE>{String.split +S +Sep ?L}</CODE>
84+
<DD>splits string <CODE>S</CODE> at all occurrences of string <CODE>Sep</CODE>
85+
and returns the resulting list <CODE>L</CODE>. <CODE>Sep</CODE> may also be
86+
<CODE>unit</CODE> in which case splits occur at all non-empty sequences of
87+
whitespace characters. It can also be <CODE>unit</CODE>, in which case
88+
splits occur between every two character
89+
<DT><CODE>{String.splitAtMost +S +Sep +N ?L}</CODE>
90+
<DD>same as above, but at most <CODE>N</CODE> splits are performed: the
91+
remainder of the string is returned as the last element of list <CODE>L</CODE>
92+
<DT><CODE>{String.lstrip +S1 +Chars ?S2}</CODE>
93+
<DD>takes a string a string <CODE>S1</CODE> and returns a string
94+
<CODE>S2</CODE> where all characters at the left of <CODE>S1</CODE>
95+
which are in <CODE>Chars</CODE> have been removed. <CODE>Chars</CODE>
96+
can also be <CODE>unit</CODE>, in which case it stands for all whitespace
97+
characters
98+
<DT><CODE>{String.rstrip +S1 +Chars ?S2}</CODE>
99+
<DD>same thing but at the right-end of <CODE>S1</CODE>
100+
<DT><CODE>{String.strip +S1 +Chars ?S2}</CODE>
101+
<DD>same thing at both ends
102+
<DT><CODE>{String.replace +S1 +Old +New ?S2}</CODE>
103+
<DD>replace every occurrence of string <CODE>Old</CODE> in <CODE>S1</CODE>
104+
by <CODE>New</CODE>
105+
<DT><CODE>{String.replaceAtMost +S1 +Old +New +N ?S2}</CODE>
106+
<DD>same as above, but replace at most <CODE>N</CODE> occurrences of
107+
<CODE>Old</CODE>
108+
</DL>
109+
110+
<HR>
111+
</BODY>
112+
</HTML>

0 commit comments

Comments
 (0)