You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Functionality in this library is largely implemented using an abstraction called a 'slice'. A slice represents a part of a string - anything from the entire string to a single character, or even no characters at all (a 0-length slice). Since a slice only has to specify an offset and a length, copying and manipulating slices is a lot less expensive than copying and manipulating the strings they reference.
4
5
5
6
To further reduce gas costs, most functions on slice that need to return a slice modify the original one instead of allocating a new one; for instance, `s.split(".")` will return the text up to the first '.', modifying s to only contain the remainder of the string after the '.'. In situations where you do not want to modify the original slice, you can make a copy first with `.copy()`, for example: `s.copy().split(".")`. Try and avoid using this idiom in loops; since Solidity has no memory management, it will result in allocating many short-lived slices that are later discarded.
@@ -8,7 +9,87 @@ Functions that return two slices come in two versions: a non-allocating version
8
9
9
10
Functions that have to copy string data will return strings rather than slices; these can be cast back to slices for further processing if required.
var len = "Unicode snowman ☃".toSlice().len(); // 17
24
+
25
+
### Splitting a string around a delimiter
26
+
var s = "foo bar baz".toSlice();
27
+
var foo = s.split(" ".toSlice());
28
+
29
+
After the above code executes, `s` is now "bar baz", and `foo` is now "foo".
30
+
31
+
### Splitting a string into an array
32
+
var s = "www.google.com".toSlice();
33
+
var delim = ".".toSlice();
34
+
var parts = new strings.slice[](s.count(delim));
35
+
for(uint i = 0; i < parts.length; i++) {
36
+
parts[i] = s.split(delim).toString();
37
+
}
38
+
39
+
### Extracting the middle part of a string
40
+
var s = "www.google.com".toSlice();
41
+
strings.slice memory part;
42
+
s.split(".".toSlice(), part); // part and return value is "www"
43
+
s.split(".".toSlice(), part); // part and return value is "google"
44
+
45
+
This approach uses less memory than the above, by reusing the slice `part` for each section of string extracted.
46
+
47
+
### Converting a slice back to a string
48
+
var myString = mySlice.toString();
49
+
50
+
### Finding and returning the first occurrence of a substring
51
+
var s = "A B C B D".toSlice();
52
+
s.find("B".toSlice()); // "B C B D"
53
+
54
+
`find` modifies `s` to contain the part of the string from the first match onwards.
55
+
56
+
### Finding and returning the last occurrence of a substring
57
+
var s = "A B C B D".toSlice();
58
+
s.rfind("B".toSlice()); // "A B C B"
59
+
60
+
`rfind` modifies `s` to contain the part of the string from the last match back to the start.
61
+
62
+
### Finding without modifying the original slice.
63
+
var s = "A B C B D".toSlice();
64
+
var substring = s.copy().rfind("B".toSlice()); // "A B C B"
65
+
66
+
`copy` lets you cheaply duplicate a slice so you don't modify the original.
67
+
68
+
### Prefix and suffix matching
69
+
var s = "A B C B D".toSlice();
70
+
s.startsWith("A".toSlice()); // True
71
+
s.endsWith("D".toSlice()); // True
72
+
s.startsWith("B".toSlice()); // False
73
+
74
+
### Removing a prefix or suffix
75
+
var s = "A B C B D".toSlice();
76
+
s.beyond("A ".toSlice()).until(" D".toSlice()); // "B C B"
77
+
78
+
`beyond` modifies `s` to contain the text after its argument; `until` modifies `s` to contain the text up to its argument. If the argument isn't found, `s` is unmodified.
79
+
80
+
### Finding and returning the string up to the first match
81
+
var s = "A B C B D".toSlice();
82
+
var needle = "B".toSlice();
83
+
var substring = s.until(s.copy().find(needle).beyond(needle));
84
+
85
+
Calling `find` on a copy of `s` returns the part of the string from `needle` onwards; calling `.beyond(needle)` removes `needle` as a prefix, and finally calling `s.until()` removes the entire end of the string, leaving everything up to and including the first match.
86
+
87
+
### Concatenating strings
88
+
var s = "abc".toSlice().concat("def".toSlice()); // "abcdef"
89
+
90
+
## Reference
91
+
92
+
### toSlice(string self) internal returns (slice)
12
93
Returns a slice containing the entire string.
13
94
14
95
Arguments:
@@ -17,7 +98,7 @@ Arguments:
17
98
18
99
Returns A newly allocated slice containing the entire string.
19
100
20
-
## copy(slice self) internal returns (slice)
101
+
###copy(slice self) internal returns (slice)
21
102
Returns a new slice containing the same data as the current slice.
22
103
23
104
Arguments:
@@ -26,7 +107,7 @@ Arguments:
26
107
27
108
Returns A new slice containing the same data as `self`.
28
109
29
-
## toString(slice self) internal returns (string)
110
+
###toString(slice self) internal returns (string)
30
111
31
112
Copies a slice to a new string.
32
113
@@ -36,7 +117,7 @@ Arguments:
36
117
37
118
Returns A newly allocated string containing the slice's text.
38
119
39
-
## len(slice self) internal returns (uint)
120
+
###len(slice self) internal returns (uint)
40
121
41
122
Returns the length in runes of the slice. Note that this operation takes time proportional to the length of the slice; avoid using it in loops, and call `slice.empty()` if you only need to know whether the slice is empty or not.
42
123
@@ -46,7 +127,7 @@ Arguments:
46
127
47
128
Returns The length of the slice in runes.
48
129
49
-
## empty(slice self) internal returns (bool)
130
+
###empty(slice self) internal returns (bool)
50
131
51
132
Returns true if the slice is empty (has a length of 0).
52
133
@@ -56,7 +137,7 @@ Arguments:
56
137
57
138
Returns True if the slice is empty, False otherwise.
Returns a positive number if `other` comes lexicographically after `self`, a negative number if it comes before, or zero if the contents of the two slices are equal. Comparison is done per-rune, on unicode codepoints.
Modifies `self` to contain everything from the first occurrence of `needle` to the end of the slice. `self` is set to the empty slice if `needle` is not found.
Modifies `self` to contain the part of the string from the start of `self` to the end of the first occurrence of `needle`. If `needle` is not found, `self` is set to the empty slice.
Splits the slice, setting `self` to everything after the first occurrence of `needle`, and `token` to everything before it. If `needle` does not occur in `self`, `self` is set to the empty slice, and `token` is set to the entirety of `self`.
Splits the slice, setting `self` to everything after the first occurrence of `needle`, and returning everything before it. If `needle` does not occur in `self`, `self` is set to the empty slice, and the entirety of `self` is returned.
203
284
@@ -208,7 +289,7 @@ Arguments:
208
289
209
290
Returns The part of `self` up to the first occurrence of `delim`.
Splits the slice, setting `self` to everything before the last occurrence of `needle`, and `token` to everything after it. If `needle` does not occur in `self`, `self` is set to the empty slice, and `token` is set to the entirety of `self`.
Splits the slice, setting `self` to everything before the last occurrence of `needle`, and returning everything after it. If `needle` does not occur in `self`, `self` is set to the empty slice, and the entirety of `self` is returned.
226
307
@@ -231,7 +312,7 @@ Arguments:
231
312
232
313
Returns The part of `self` after the last occurrence of `delim`.
0 commit comments