forked from purescript/purescript-strings
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathString.cc
More file actions
333 lines (306 loc) · 9.41 KB
/
String.cc
File metadata and controls
333 lines (306 loc) · 9.41 KB
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
///////////////////////////////////////////////////////////////////////////////
//
// Module : String.cc
// Copyright : (c) Andy Arvanitis 2016
// License : MIT
//
// Maintainer : Andy Arvanitis <andy.arvanitis@gmail.com>
// Stability : experimental
// Portability :
//
// String FFI functions
//
///////////////////////////////////////////////////////////////////////////////
//
#include <algorithm>
#include <locale>
#include <codecvt>
#include <regex>
#include "String.hh"
#include "Data.Char/Char.hh"
namespace Data_String {
static std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf32conv;
static constexpr auto kRegexOpts =
std::regex_constants::ECMAScript | std::regex_constants::collate;
// foreign import _charAt
// :: (forall a. a -> Maybe a)
// -> (forall a. Maybe a)
// -> Int
// -> String
// -> Maybe Char
//
auto _charAt(const any& just,
const any& nothing,
const int i,
const string& s) -> any {
if (i < 0 or i >= s.size()) {
return nothing;
} else {
return just(UChar_At(i, s));
}
}
// foreign import singleton :: Char -> String
//
auto singleton(char32_t c) -> string {
return utf32conv.to_bytes(c);
}
// foreign import _charCodeAt
// :: (forall a. a -> Maybe a)
// -> (forall a. Maybe a)
// -> Int
// -> String
// -> Maybe Int
//
auto _charCodeAt(const any& just,
const any& nothing,
const int i,
const string& s) -> any {
if (i < 0 or i >= s.size()) {
return nothing;
} else {
return just(Data_Char::toCharCode(UChar_At(i, s)));
}
}
// foreign import _toChar
// :: (forall a. a -> Maybe a)
// -> (forall a. Maybe a)
// -> String
// -> Maybe Char
//
auto _toChar(const any& just,
const any& nothing,
const string& s) -> any {
const auto utf32 = utf32conv.from_bytes(s);
return utf32.size() == 1 ? just(utf32.front()) : nothing;
}
// foreign import fromCharArray :: Array Char -> String
//
auto fromCharArray(const any::array& xs) -> string {
std::u32string utf32;
utf32.reserve(xs.size());
for (auto it = xs.cbegin(), end = xs.cend(); it != end; it++) {
utf32.push_back(*it);
}
return utf32conv.to_bytes(utf32);
}
// foreign import _indexOf
// :: (forall a. a -> Maybe a)
// -> (forall a. Maybe a)
// -> Pattern
// -> String
// -> Maybe Int
//
auto _indexOf(const any& just,
const any& nothing,
const string& x,
const string& s) -> any {
const auto i = s.find(x);
assert(i == string::npos || i <= std::numeric_limits<int>::max());
return i == string::npos ? nothing : just(static_cast<int>(i));
}
// foreign import _indexOf'
// :: (forall a. a -> Maybe a)
// -> (forall a. Maybe a)
// -> Pattern
// -> Int
// -> String
// -> Maybe Int
//
auto _indexOf$prime(const any& just,
const any& nothing,
const string& x,
const int startAt,
const string& s) -> any {
if (startAt < 0) {
return nothing;
}
const auto i = s.find(x, startAt);
assert(i == string::npos || i <= std::numeric_limits<int>::max());
return i == string::npos ? nothing : just(static_cast<int>(i));
}
// foreign import _lastIndexOf
// :: (forall a. a -> Maybe a)
// -> (forall a. Maybe a)
// -> Pattern
// -> String
// -> Maybe Int
//
auto _lastIndexOf(const any& just,
const any& nothing,
const string& x,
const string& s) -> any {
const auto i = s.rfind(x);
assert(i == string::npos || i <= std::numeric_limits<int>::max());
return i == string::npos ? nothing : just(static_cast<int>(i));
}
// foreign import _lastIndexOf'
// :: (forall a. a -> Maybe a)
// -> (forall a. Maybe a)
// -> Pattern
// -> Int
// -> String
// -> Maybe Int
//
auto _lastIndexOf$prime(const any& just,
const any& nothing,
const string& x,
const int startAt,
const string& s) -> any {
if (startAt < 0 || startAt > s.size()) {
return nothing;
}
const auto i = s.rfind(x, startAt);
assert(i == string::npos || i <= std::numeric_limits<int>::max());
return i == string::npos ? nothing : just(static_cast<int>(i));
}
// foreign import length :: String -> Int
//
auto length(const string& s) -> int {
const auto sz = s.size();
assert(sz <= std::numeric_limits<int>::max());
return static_cast<int>(sz);
}
// foreign import _localeCompare
// :: Ordering
// -> Ordering
// -> Ordering
// -> String
// -> String
// -> Ordering
//
auto _localeCompare(const any& lt,
const any& eq,
const any& gt,
const string& s1,
const string& s2) -> any {
// TODO: more testing needed
const auto& collate = std::use_facet<std::collate<char>>(std::locale());
const auto result = collate.compare(s1.c_str(), s1.c_str() + s1.size(),
s2.c_str(), s2.c_str() + s2.size());
return result < 0 ? lt : result > 0 ? gt : eq;
}
// foreign import replace :: Pattern -> Replacement -> String -> String
//
auto replace(const string& s1, const string& s2, const string& s3) -> string {
string s(s3);
const auto found = s.find(s1);
return found == string::npos ? s : s.replace(found, s1.size(), s2);
}
// foreign import replaceAll :: Pattern -> Replacement -> String -> String
//
auto replaceAll(const string& s1, const string& s2, const string& s3) -> string {
string s(s3);
for (string::size_type pos = 0, found = string::npos;
(found = s.find(s1, pos)) != string::npos;
pos += s2.size()) {
s.replace(found, s1.size(), s2);
}
return s;
}
// foreign import take :: Int -> String -> String
//
auto take(const int n, const string& s) -> string {
return s.substr(0, n < 0 ? 0 : n);
}
// foreign import drop :: Int -> String -> String
//
auto drop(const int n, const string& s) -> string {
return n <= 0 ? s : n >= s.size() ? "" : s.substr(n);
}
// foreign import count :: (Char -> Boolean) -> String -> Int
//
auto count(const any& p, const string& s) -> int {
const auto utf32 = utf32conv.from_bytes(s);
decltype(utf32)::size_type i = 0;
while (i < utf32.size() && p(utf32[i])) {
i++;
}
return i;
}
// foreign import split :: Pattern -> String -> Array String
//
auto split(const string& sep, string s) -> any::array {
any::array result;
if (not sep.empty()) {
string::size_type n;
while (n = s.find(sep), n != string::npos) {
result.emplace_back(s.substr(0, n));
s = s.substr(n + sep.size());
}
result.emplace_back(s.substr(0, n));
} else {
const auto utf32 = utf32conv.from_bytes(s);
for (auto it = utf32.cbegin(), end = utf32.cend(); it != end; it++) {
result.emplace_back(utf32conv.to_bytes(*it));
}
}
return result;
}
// foreign import _splitAt :: (forall a. a -> Maybe a)
// -> (forall a. Maybe a)
// -> Int
// -> String
// -> Maybe (Array String)
//
auto _splitAt(const any& just,
const any& nothing,
const int i,
const string& s) -> any {
return i >= 0 && i < s.size() ? just(any::array{s.substr(0, i), s.substr(i)})
: nothing;
}
// foreign import toCharArray :: String -> Array Char
//
auto toCharArray(const string& s) -> any::array {
any::array result;
const auto utf32 = utf32conv.from_bytes(s);
for (auto it = utf32.cbegin(), end = utf32.cend(); it != end; it++) {
result.emplace_back(*it);
}
return result;
}
// foreign import toLower :: String -> String
//
auto toLower(const string& s) -> string {
// Note: does not convert unicode characters
string result;
result.reserve(s.size());
for (auto it = s.cbegin(), end = s.cend(); it != end; it++) {
result.push_back(std::tolower(*it));
}
return result;
}
// foreign import toUpper :: String -> String
//
auto toUpper(const string& s) -> string {
// Note: does not convert unicode characters
string result;
result.reserve(s.size());
for (auto it = s.cbegin(), end = s.cend(); it != end; it++) {
result.push_back(std::toupper(*it));
}
return result;
}
// foreign import trim :: String -> String
//
auto trim(const string& s) -> string {
static const auto expr = std::regex("^\\s+|\\s+$", kRegexOpts);
return std::regex_replace(s, expr, "");
}
// foreign import joinWith :: String -> Array String -> String
//
auto joinWith(const string& s, const any::array& xs) -> string {
const auto sz = xs.size();
string result;
if (sz > 0) {
result = static_cast<const string&>(xs.front());
}
if (sz > 1) {
for (auto it = xs.cbegin() + 1, end = xs.cend(); it != end; it++) {
result.append(s);
result.append(static_cast<const string&>(*it));
}
}
return result;
}
}