forked from sass/node-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn_strings.cpp
254 lines (224 loc) · 7.75 KB
/
fn_strings.cpp
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
// sass.hpp must go before all system headers to get the
// __EXTENSIONS__ fix on Solaris.
#include "sass.hpp"
#include <cctype>
#include "utf8.h"
#include "ast.hpp"
#include "fn_utils.hpp"
#include "fn_strings.hpp"
namespace Sass {
namespace Functions {
void handle_utf8_error (const ParserState& pstate, Backtraces traces)
{
try {
throw;
}
catch (utf8::invalid_code_point&) {
std::string msg("utf8::invalid_code_point");
error(msg, pstate, traces);
}
catch (utf8::not_enough_room&) {
std::string msg("utf8::not_enough_room");
error(msg, pstate, traces);
}
catch (utf8::invalid_utf8&) {
std::string msg("utf8::invalid_utf8");
error(msg, pstate, traces);
}
catch (...) { throw; }
}
///////////////////
// STRING FUNCTIONS
///////////////////
Signature unquote_sig = "unquote($string)";
BUILT_IN(sass_unquote)
{
AST_Node_Obj arg = env["$string"];
if (String_Quoted* string_quoted = Cast<String_Quoted>(arg)) {
String_Constant* result = SASS_MEMORY_NEW(String_Constant, pstate, string_quoted->value());
// remember if the string was quoted (color tokens)
result->is_delayed(true); // delay colors
return result;
}
else if (String_Constant* str = Cast<String_Constant>(arg)) {
return str;
}
else if (Value* ex = Cast<Value>(arg)) {
Sass_Output_Style oldstyle = ctx.c_options.output_style;
ctx.c_options.output_style = SASS_STYLE_NESTED;
std::string val(arg->to_string(ctx.c_options));
val = Cast<Null>(arg) ? "null" : val;
ctx.c_options.output_style = oldstyle;
deprecated_function("Passing " + val + ", a non-string value, to unquote()", pstate);
return ex;
}
throw std::runtime_error("Invalid Data Type for unquote");
}
Signature quote_sig = "quote($string)";
BUILT_IN(sass_quote)
{
const String_Constant* s = ARG("$string", String_Constant);
String_Quoted *result = SASS_MEMORY_NEW(
String_Quoted, pstate, s->value(),
/*q=*/'\0', /*keep_utf8_escapes=*/false, /*skip_unquoting=*/true);
result->quote_mark('*');
return result;
}
Signature str_length_sig = "str-length($string)";
BUILT_IN(str_length)
{
size_t len = std::string::npos;
try {
String_Constant* s = ARG("$string", String_Constant);
len = UTF_8::code_point_count(s->value(), 0, s->value().size());
}
// handle any invalid utf8 errors
// other errors will be re-thrown
catch (...) { handle_utf8_error(pstate, traces); }
// return something even if we had an error (-1)
return SASS_MEMORY_NEW(Number, pstate, (double)len);
}
Signature str_insert_sig = "str-insert($string, $insert, $index)";
BUILT_IN(str_insert)
{
std::string str;
try {
String_Constant* s = ARG("$string", String_Constant);
str = s->value();
String_Constant* i = ARG("$insert", String_Constant);
std::string ins = i->value();
double index = ARGVAL("$index");
size_t len = UTF_8::code_point_count(str, 0, str.size());
if (index > 0 && index <= len) {
// positive and within string length
str.insert(UTF_8::offset_at_position(str, static_cast<size_t>(index) - 1), ins);
}
else if (index > len) {
// positive and past string length
str += ins;
}
else if (index == 0) {
str = ins + str;
}
else if (std::abs(index) <= len) {
// negative and within string length
index += len + 1;
str.insert(UTF_8::offset_at_position(str, static_cast<size_t>(index)), ins);
}
else {
// negative and past string length
str = ins + str;
}
if (String_Quoted* ss = Cast<String_Quoted>(s)) {
if (ss->quote_mark()) str = quote(str);
}
}
// handle any invalid utf8 errors
// other errors will be re-thrown
catch (...) { handle_utf8_error(pstate, traces); }
return SASS_MEMORY_NEW(String_Quoted, pstate, str);
}
Signature str_index_sig = "str-index($string, $substring)";
BUILT_IN(str_index)
{
size_t index = std::string::npos;
try {
String_Constant* s = ARG("$string", String_Constant);
String_Constant* t = ARG("$substring", String_Constant);
std::string str = s->value();
std::string substr = t->value();
size_t c_index = str.find(substr);
if(c_index == std::string::npos) {
return SASS_MEMORY_NEW(Null, pstate);
}
index = UTF_8::code_point_count(str, 0, c_index) + 1;
}
// handle any invalid utf8 errors
// other errors will be re-thrown
catch (...) { handle_utf8_error(pstate, traces); }
// return something even if we had an error (-1)
return SASS_MEMORY_NEW(Number, pstate, (double)index);
}
Signature str_slice_sig = "str-slice($string, $start-at, $end-at:-1)";
BUILT_IN(str_slice)
{
std::string newstr;
try {
String_Constant* s = ARG("$string", String_Constant);
double start_at = ARGVAL("$start-at");
double end_at = ARGVAL("$end-at");
String_Quoted* ss = Cast<String_Quoted>(s);
std::string str(s->value());
size_t size = utf8::distance(str.begin(), str.end());
if (!Cast<Number>(env["$end-at"])) {
end_at = -1;
}
if (end_at == 0 || (end_at + size) < 0) {
if (ss && ss->quote_mark()) newstr = quote("");
return SASS_MEMORY_NEW(String_Quoted, pstate, newstr);
}
if (end_at < 0) {
end_at += size + 1;
if (end_at == 0) end_at = 1;
}
if (end_at > size) { end_at = (double)size; }
if (start_at < 0) {
start_at += size + 1;
if (start_at < 0) start_at = 0;
}
else if (start_at == 0) { ++ start_at; }
if (start_at <= end_at)
{
std::string::iterator start = str.begin();
utf8::advance(start, start_at - 1, str.end());
std::string::iterator end = start;
utf8::advance(end, end_at - start_at + 1, str.end());
newstr = std::string(start, end);
}
if (ss) {
if(ss->quote_mark()) newstr = quote(newstr);
}
}
// handle any invalid utf8 errors
// other errors will be re-thrown
catch (...) { handle_utf8_error(pstate, traces); }
return SASS_MEMORY_NEW(String_Quoted, pstate, newstr);
}
Signature to_upper_case_sig = "to-upper-case($string)";
BUILT_IN(to_upper_case)
{
String_Constant* s = ARG("$string", String_Constant);
std::string str = s->value();
for (size_t i = 0, L = str.length(); i < L; ++i) {
if (Sass::Util::isAscii(str[i])) {
str[i] = std::toupper(str[i]);
}
}
if (String_Quoted* ss = Cast<String_Quoted>(s)) {
String_Quoted* cpy = SASS_MEMORY_COPY(ss);
cpy->value(str);
return cpy;
} else {
return SASS_MEMORY_NEW(String_Quoted, pstate, str);
}
}
Signature to_lower_case_sig = "to-lower-case($string)";
BUILT_IN(to_lower_case)
{
String_Constant* s = ARG("$string", String_Constant);
std::string str = s->value();
for (size_t i = 0, L = str.length(); i < L; ++i) {
if (Sass::Util::isAscii(str[i])) {
str[i] = std::tolower(str[i]);
}
}
if (String_Quoted* ss = Cast<String_Quoted>(s)) {
String_Quoted* cpy = SASS_MEMORY_COPY(ss);
cpy->value(str);
return cpy;
} else {
return SASS_MEMORY_NEW(String_Quoted, pstate, str);
}
}
}
}