File: Strings.k

package info (click to toggle)
kaya 0.2.0-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,012 kB
  • ctags: 1,307
  • sloc: cpp: 6,691; haskell: 4,833; sh: 2,868; yacc: 768; makefile: 700; perl: 87
file content (228 lines) | stat: -rw-r--r-- 5,511 bytes parent folder | download
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
/** -*-C-*-ish
    Kaya standard library
    Copyright (C) 2004, 2005 Edwin Brady

    This file is distributed under the terms of the GNU Lesser General
    Public Licence. See COPYING for licence.
*/

module Strings;

import Regex;
import Prelude;

%include "string.h";
%include "stdlib.h";

foreign "stdfuns.o" {
    String b64enc(String block, Int len) = b64enc;
    String b64dec(String str, intval len) = b64dec;
    public Bool elem(Char c, String str) = do_wcschr;
    "Returns the position of the first occurence of the character in the string (or the length of the string if the character is not in the string)"
    public Int firstOccurs(Char c, String str) = do_wcscspn;
}

public Exception StringEmpty = Exception("Can't take head of an empty string",210);

"Split a String into words.
Splits into the whitespace separated words."
public [String] words(String x)
{
    [String] all = [];
    str = x;
    current = "";
    while (str!="") {
	c = head(str);
	str = tail(str);
	if (isWhitespace(c)) {
	    if (current!="") {
		all[size(all)]=current;
		current = "";
	    }
	}
	else {
	    current = current + String(c);
	}
    }
    if (current!="") {
	all[size(all)]=current;
    }
    return all;
}

"Split a String into substrings
Splits by a given delimiter or newline if none is given."
[String] splitBy(String x, Bool(Char) d=isLineEnding)
{
    [String] all = [];
    str = x;
    current = "";
    while (str!="") {
	c = head(str);
	str = tail(str);
	if (d(c)) {
	    all[size(all)]=current;
	    current = "";
	}
	else {
	    current = current + String(c);
	}
    }
    if (current!="") {
	all[size(all)]=current;
    }
    return all;
}

"Splits a string into lines"
public [String] lines(String x) {
  // otherwise windows line endings get split wrongly
  replace("\r\n","\n",x,[Global]);
  return splitBy(x,isLineEnding);
}

"Join a list of words into a String."
public String unwords([String] xs) = Strings::join(xs,' ');

"DEPRECATED synonym for join"
public String unlines([String] xs) = Strings::join(xs,'\n');

"DEPRECATED synonym for join"
public String concatWith([String] xs, Char c='\n') = Strings::join(xs,c);

"Join a list of words into a String with the given separator
or newline if none is given."
public String join([String] xs, Char c='\n')
{
  if (size(xs) == 0) { 
    return "";
  } // needed to avoid substr exception
    acc="";
    for x in xs {
	acc += x + String(c);
    }
    // since the separator is a single character, this trims the unwanted trailing separator
    return substr(acc,0,length(acc)-1);
}

"Join a list of words into a String with the given separator"
public String join([String] xs, String s)
{
  if (size(xs) == 0) { 
    return "";
  } // needed to avoid substr exception
    acc="";
    for x in xs {
	acc += x + s;
    }
    // trims the unwanted trailing separator
    return substr(acc,0,length(acc)-length(s));
}


"Get the first character in a String"
public Char head(String s) {
  /*  if (length(s) == 0) { // getIndex does this check
    throw(StringEmpty);
    }*/
  return getIndex(s,0);
}

"Get all but the first character of a String."
public String tail(String str) = strEnd(str,1);

"DEPRECATED, use 'array' coercion instead.
Turn a String into an array of Char."
public [Char] unpackString(String x)
{
    xs = [];
    for(i=0;i<length(x);i=i+1) {
	push(xs,getIndex(x,i));
    }
    return xs;
}

"DEPRECATED, use 'string' coercion instead.
Turn an array of Char into a String."
public String packString([Char] x)
{
    str = createString(size(x));
    for c in x {
	str=str+String(c);
    }
    return str;
}

"Remove characters from the end of a String as determined by the trimming function.
isWhitespace is the default function"
public Void rtrim(var String x, Bool(Char) fn=isWhitespace)
{
    while (length(x) > 0 && fn(getIndex(x,length(x)-1)))
    {
      x=substr(x,0,length(x)-1);
    }
}

"Remove characters from the start of a String as determined by the trimming function.
isWhitespace is the default function"
public Void ltrim(var String x, Bool(Char) fn=isWhitespace)
{
    while (length(x) > 0 && fn(head(x)))
    {
	  x=tail(x);
    }
}

"Remove characters from the start and end of a String as determined by the trimming function.
isWhitespace is the default function"
public Void trim(var String x, Bool(Char) fn=isWhitespace) 
{
    ltrim(x,fn);
    rtrim(x,fn);
}

"DEPRECATED synonym for rtrim(x,@isLineEnding)"
public Void chomp(var String x) = rtrim(x,@isLineEnding);

"DEPRECATED synonym for toLowercase
Convert a string to lower case.
Returns the string <em>x</em> in lower case, <em>x</em> is unmodified."
public String lc(String x)
{
    newx = createString(length(x));
    for(i=0;i<length(x);i=i+1) {
	newx += String(tolower(getIndex(x,i)));
    }
    return newx;
}

"DEPRECATED synonym for toUppercase
Convert a string to upper case.
Returns the string <em>x</em> in lower case, <em>x</em> is unmodified."
public String uc(String x)
{
    newx = createString(length(x));
    for(i=0;i<length(x);i=i+1) {
	newx += String(toupper(getIndex(x,i)));
    }
    return newx;
}

"Encode a string in base64"
public String base64Encode(String str)
{
    return b64enc(str,length(str));
}

"DEPRECATED synonym for base64Encode"
public String base64encode(String str) = base64Encode(str);

"Decode a base64 encoded string"
public String base64Decode(String str)
{
    len=0;
    return b64dec(str,len);
}

"DEPRECATED synonym for base64Decode"
public String base64decode(String str) = base64Decode(str);