Skip to content

Commit 3129b17

Browse files
committed
Updated readme; deleted stringutils
1 parent 183f1f8 commit 3129b17

File tree

3 files changed

+106
-302
lines changed

3 files changed

+106
-302
lines changed

README.md

Lines changed: 106 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
# String & slice utility library for Solidity
3+
## Overview
34
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.
45

56
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
89

910
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.
1011

11-
## toSlice(string self) internal returns (slice)
12+
## Examples
13+
### Basic usage
14+
import "github.com/Arachnid/solidity-stringutils/strings.sol";
15+
16+
contract Contract {
17+
using strings for *;
18+
19+
// ...
20+
}
21+
22+
### Getting the character length of a string
23+
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)
1293
Returns a slice containing the entire string.
1394

1495
Arguments:
@@ -17,7 +98,7 @@ Arguments:
1798

1899
Returns A newly allocated slice containing the entire string.
19100

20-
## copy(slice self) internal returns (slice)
101+
### copy(slice self) internal returns (slice)
21102
Returns a new slice containing the same data as the current slice.
22103

23104
Arguments:
@@ -26,7 +107,7 @@ Arguments:
26107

27108
Returns A new slice containing the same data as `self`.
28109

29-
## toString(slice self) internal returns (string)
110+
### toString(slice self) internal returns (string)
30111

31112
Copies a slice to a new string.
32113

@@ -36,7 +117,7 @@ Arguments:
36117

37118
Returns A newly allocated string containing the slice's text.
38119

39-
## len(slice self) internal returns (uint)
120+
### len(slice self) internal returns (uint)
40121

41122
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.
42123

@@ -46,7 +127,7 @@ Arguments:
46127

47128
Returns The length of the slice in runes.
48129

49-
## empty(slice self) internal returns (bool)
130+
### empty(slice self) internal returns (bool)
50131

51132
Returns true if the slice is empty (has a length of 0).
52133

@@ -56,7 +137,7 @@ Arguments:
56137

57138
Returns True if the slice is empty, False otherwise.
58139

59-
## compare(slice self, slice other) internal returns (int)
140+
### compare(slice self, slice other) internal returns (int)
60141

61142
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.
62143

@@ -67,7 +148,7 @@ Arguments:
67148

68149
Returns The result of the comparison.
69150

70-
## equals(slice self, slice other) internal returns (bool)
151+
### equals(slice self, slice other) internal returns (bool)
71152

72153
Returns true if the two slices contain the same text.
73154

@@ -78,7 +159,7 @@ Arguments:
78159

79160
Returns True if the slices are equal, false otherwise.
80161

81-
## nextRune(slice self, slice rune) internal returns (slice)
162+
### nextRune(slice self, slice rune) internal returns (slice)
82163

83164
Extracts the first rune in the slice into `rune`, advancing the slice to point to the next rune and returning `self`.
84165

@@ -89,7 +170,7 @@ Arguments:
89170

90171
Returns `rune`.
91172

92-
## nextRune(slice self) internal returns (slice ret)
173+
### nextRune(slice self) internal returns (slice ret)
93174

94175
Returns the first rune in the slice, advancing the slice to point to the next rune.
95176

@@ -99,7 +180,7 @@ Arguments:
99180

100181
Returns A slice containing only the first rune from `self`.
101182

102-
## ord(slice self) internal returns (uint ret)
183+
### ord(slice self) internal returns (uint ret)
103184

104185
Returns the number of the first codepoint in the slice.
105186

@@ -109,7 +190,7 @@ Arguments:
109190

110191
Returns The number of the first codepoint in the slice.
111192

112-
## keccak(slice self) internal returns (bytes32 ret)
193+
### keccak(slice self) internal returns (bytes32 ret)
113194

114195
Returns the keccak-256 hash of the slice.
115196

@@ -119,7 +200,7 @@ Arguments:
119200

120201
Returns The hash of the slice.
121202

122-
## startsWith(slice self, slice needle) internal returns (bool)
203+
### startsWith(slice self, slice needle) internal returns (bool)
123204

124205
Returns true if `self` starts with `needle`.
125206

@@ -130,7 +211,7 @@ Arguments:
130211

131212
Returns True if the slice starts with the provided text, false otherwise.
132213

133-
## beyond(slice self, slice needle) internal returns (slice)
214+
### beyond(slice self, slice needle) internal returns (slice)
134215

135216
If `self` starts with `needle`, `needle` is removed from the beginning of `self`. Otherwise, `self` is unmodified.
136217

@@ -141,7 +222,7 @@ Arguments:
141222

142223
Returns `self`
143224

144-
## endsWith(slice self, slice needle) internal returns (bool)
225+
### endsWith(slice self, slice needle) internal returns (bool)
145226

146227
Returns true if the slice ends with `needle`.
147228

@@ -152,7 +233,7 @@ Arguments:
152233

153234
Returns True if the slice starts with the provided text, false otherwise.
154235

155-
## until(slice self, slice needle) internal returns (slice)
236+
### until(slice self, slice needle) internal returns (slice)
156237

157238
If `self` ends with `needle`, `needle` is removed from the end of `self`. Otherwise, `self` is unmodified.
158239

@@ -163,7 +244,7 @@ Arguments:
163244

164245
Returns `self`
165246

166-
## find(slice self, slice needle) internal returns (slice)
247+
### find(slice self, slice needle) internal returns (slice)
167248

168249
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.
169250

@@ -174,7 +255,7 @@ Arguments:
174255

175256
Returns `self`.
176257

177-
## rfind(slice self, slice needle) internal returns (slice)
258+
### rfind(slice self, slice needle) internal returns (slice)
178259

179260
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.
180261

@@ -185,7 +266,7 @@ Arguments:
185266

186267
Returns `self`.
187268

188-
## split(slice self, slice needle, slice token) internal returns (slice)
269+
### split(slice self, slice needle, slice token) internal returns (slice)
189270

190271
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`.
191272

@@ -197,7 +278,7 @@ Arguments:
197278

198279
Returns `token`.
199280

200-
## split(slice self, slice needle) internal returns (slice token)
281+
### split(slice self, slice needle) internal returns (slice token)
201282

202283
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.
203284

@@ -208,7 +289,7 @@ Arguments:
208289

209290
Returns The part of `self` up to the first occurrence of `delim`.
210291

211-
## rsplit(slice self, slice needle, slice token) internal returns (slice)
292+
### rsplit(slice self, slice needle, slice token) internal returns (slice)
212293

213294
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`.
214295

@@ -220,7 +301,7 @@ Arguments:
220301

221302
Returns `token`.
222303

223-
## rsplit(slice self, slice needle) internal returns (slice token)
304+
### rsplit(slice self, slice needle) internal returns (slice token)
224305

225306
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.
226307

@@ -231,7 +312,7 @@ Arguments:
231312

232313
Returns The part of `self` after the last occurrence of `delim`.
233314

234-
## count(slice self, slice needle) internal returns (uint count)
315+
### count(slice self, slice needle) internal returns (uint count)
235316

236317
Counts the number of nonoverlapping occurrences of `needle` in `self`.
237318

@@ -242,7 +323,7 @@ Arguments:
242323

243324
Returns The number of occurrences of `needle` found in `self`.
244325

245-
## contains(slice self, slice needle) internal returns (bool)
326+
### contains(slice self, slice needle) internal returns (bool)
246327

247328
Returns True if `self` contains `needle`.
248329

@@ -253,7 +334,7 @@ Arguments:
253334

254335
Returns True if `needle` is found in `self`, false otherwise.
255336

256-
## concat(slice self, slice other) internal returns (string)
337+
### concat(slice self, slice other) internal returns (string)
257338

258339
Returns a newly allocated string containing the concatenation of `self` and `other`.
259340

@@ -264,7 +345,7 @@ Arguments:
264345

265346
Returns The concatenation of the two strings.
266347

267-
## join(slice self, slice[] parts) internal returns (string)
348+
### join(slice self, slice[] parts) internal returns (string)
268349

269350
Joins an array of slices, using `self` as a delimiter, returning a newly allocated string.
270351

0 commit comments

Comments
 (0)