-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathStringFunctions.php
More file actions
219 lines (178 loc) · 6.31 KB
/
Copy pathStringFunctions.php
File metadata and controls
219 lines (178 loc) · 6.31 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
<?php
/**
* SCSSPHP
*
* @copyright 2012-2020 Leaf Corcoran
*
* @license http://opensource.org/licenses/MIT MIT
*
* @link http://scssphp.github.io/scssphp
*/
namespace ScssPhp\ScssPhp\Function;
use ScssPhp\ScssPhp\Util\StringUtil;
use ScssPhp\ScssPhp\Value\SassNull;
use ScssPhp\ScssPhp\Value\SassNumber;
use ScssPhp\ScssPhp\Value\SassString;
use ScssPhp\ScssPhp\Value\Value;
/**
* @internal
*/
final class StringFunctions
{
private static ?int $previousId = null;
/**
* @param list<Value> $arguments
*/
public static function unquote(array $arguments): Value
{
$string = $arguments[0]->assertString('string');
if (!$string->hasQuotes()) {
return $string;
}
return new SassString($string->getText(), false);
}
/**
* @param list<Value> $arguments
*/
public static function quote(array $arguments): Value
{
$string = $arguments[0]->assertString('string');
if ($string->hasQuotes()) {
return $string;
}
return new SassString($string->getText(), true);
}
/**
* @param list<Value> $arguments
*/
public static function length(array $arguments): Value
{
$string = $arguments[0]->assertString('string');
return SassNumber::create($string->getSassLength());
}
/**
* @param list<Value> $arguments
*/
public static function insert(array $arguments): Value
{
$string = $arguments[0]->assertString('string');
$insert = $arguments[1]->assertString('insert');
$index = $arguments[2]->assertNumber('index');
$index->assertNoUnits('index');
$indexInt = $index->assertInt('index');
// str-insert has unusual behavior for negative inputs. It guarantees that
// the `$insert` string is at `$index` in the result, which means that we
// want to insert before `$index` if it's positive and after if it's
// negative.
if ($indexInt < 0) {
// +1 because negative indexes start counting from -1 rather than 0, and
// another +1 because we want to insert *after* that index.
$indexInt = max($string->getSassLength() + $indexInt + 2, 0);
}
$codepointIndex = self::codepointForIndex($indexInt, $string->getSassLength());
return new SassString(
mb_substr($string->getText(), 0, $codepointIndex) . $insert->getText() . mb_substr($string->getText(), $codepointIndex),
$string->hasQuotes()
);
}
/**
* @param list<Value> $arguments
*/
public static function index(array $arguments): Value
{
$string = $arguments[0]->assertString('string');
$substring = $arguments[1]->assertString('substring');
$codepointIndex = mb_strpos($string->getText(), $substring->getText());
if ($codepointIndex === false) {
return SassNull::create();
}
return SassNumber::create($codepointIndex + 1);
}
/**
* @param list<Value> $arguments
*/
public static function slice(array $arguments): Value
{
$string = $arguments[0]->assertString('string');
$start = $arguments[1]->assertNumber('start-at');
$end = $arguments[2]->assertNumber('end-at');
$start->assertNoUnits('start-at');
$end->assertNoUnits('end-at');
$lengthInCodepoints = $string->getSassLength();
// No matter what the start index is, an end index of 0 will produce an
// empty string.
$endInt = $end->assertInt();
if ($endInt === 0) {
return new SassString('', $string->hasQuotes());
}
$startCodepoint = self::codepointForIndex($start->assertInt(), $lengthInCodepoints);
$endCodepoint = self::codepointForIndex($endInt, $lengthInCodepoints, true);
if ($endCodepoint === $lengthInCodepoints) {
$endCodepoint--;
}
if ($endCodepoint < $startCodepoint) {
return new SassString('', $string->hasQuotes());
}
return new SassString(
mb_substr($string->getText(), $startCodepoint, $endCodepoint + 1 - $startCodepoint),
$string->hasQuotes()
);
}
/**
* @param list<Value> $arguments
*/
public static function toUpperCase(array $arguments): Value
{
$string = $arguments[0]->assertString('string');
return new SassString(StringUtil::toAsciiUpperCase($string->getText()), $string->hasQuotes());
}
/**
* @param list<Value> $arguments
*/
public static function toLowerCase(array $arguments): Value
{
$string = $arguments[0]->assertString('string');
return new SassString(StringUtil::toAsciiLowerCase($string->getText()), $string->hasQuotes());
}
/**
* @param list<Value> $arguments
*/
public static function uniqueId(array $arguments): Value
{
if (self::$previousId === null) {
self::$previousId = random_int(0, 36 ** 6);
}
// Make it difficult to guess the next ID by randomizing the increase.
self::$previousId += random_int(0, 36) + 1;
if (self::$previousId > 36 ** 6) {
self::$previousId %= 36 ** 6;
}
// The leading "u" ensures that the result is a valid identifier.
return new SassString('u' . str_pad(base_convert((string) self::$previousId, 10, 36), 6, '0', STR_PAD_LEFT), false);
}
/**
* Converts a Sass string index into a codepoint index into a string which
* has length $lengthInCodepoints measured in codepoints (with `mb_strlen`).
*
* A Sass string index is one-based, and uses negative numbers to count
* backwards from the end of the string.
*
* If $index is negative and it points before the beginning of
* $lengthInCodepoints, this will return `0` if $allowNegative is `false` and
* the index if it's `true`.
*/
private static function codepointForIndex(int $index, int $lengthInCodepoints, bool $allowNegative = false): int
{
if ($index === 0) {
return 0;
}
if ($index > 0) {
return min($index - 1, $lengthInCodepoints);
}
$result = $lengthInCodepoints + $index;
if ($result < 0 && !$allowNegative) {
return 0;
}
return $result;
}
}