prettier-plugin-solidity 1.0.0-beta.19 → 1.0.0-beta.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/README.md +22 -13
  2. package/package.json +13 -13
  3. package/prettier-plugin-solidity-1.0.0-beta.19.tgz +0 -0
  4. package/src/binary-operator-printers/index.js +1 -1
  5. package/src/nodes/Continue.js +5 -0
  6. package/src/nodes/FunctionCall.js +17 -14
  7. package/src/nodes/InlineAssemblyStatement.js +17 -1
  8. package/src/nodes/ModifierDefinition.js +5 -3
  9. package/src/nodes/UsingForDeclaration.js +19 -3
  10. package/src/nodes/index.js +2 -2
  11. package/.github/FUNDING.yml +0 -1
  12. package/.github/dependabot.yml +0 -23
  13. package/.github/workflows/CI.yml +0 -83
  14. package/assets/telegram-badge.svg +0 -29
  15. package/scripts/generateIndexes.js +0 -16
  16. package/scripts/makeData.js +0 -23
  17. package/src/nodes/TypeNameExpression.js +0 -5
  18. package/tests/config/.prettierrc +0 -5
  19. package/tests/config/format-test.js +0 -458
  20. package/tests/config/require-prettier.js +0 -5
  21. package/tests/config/require-standalone.js +0 -25
  22. package/tests/config/setup.js +0 -7
  23. package/tests/config/utils/check-parsers.js +0 -207
  24. package/tests/config/utils/compile-contract.js +0 -36
  25. package/tests/config/utils/consistent-end-of-line.js +0 -13
  26. package/tests/config/utils/create-snapshot.js +0 -98
  27. package/tests/config/utils/stringify-options-for-title.js +0 -15
  28. package/tests/config/utils/visualize-end-of-line.js +0 -18
  29. package/tests/config/utils/visualize-range.js +0 -40
  30. package/tests/format/AddressPayable/AddressPayable.sol +0 -10
  31. package/tests/format/AddressPayable/__snapshots__/jsfmt.spec.js.snap +0 -37
  32. package/tests/format/AddressPayable/jsfmt.spec.js +0 -1
  33. package/tests/format/AllSolidityFeatures/AllSolidityFeatures.sol +0 -533
  34. package/tests/format/AllSolidityFeatures/__snapshots__/jsfmt.spec.js.snap +0 -1146
  35. package/tests/format/AllSolidityFeatures/jsfmt.spec.js +0 -1
  36. package/tests/format/Arrays/Arrays.sol +0 -17
  37. package/tests/format/Arrays/__snapshots__/jsfmt.spec.js.snap +0 -63
  38. package/tests/format/Arrays/jsfmt.spec.js +0 -1
  39. package/tests/format/Assembly/Assembly.sol +0 -180
  40. package/tests/format/Assembly/__snapshots__/jsfmt.spec.js.snap +0 -406
  41. package/tests/format/Assembly/jsfmt.spec.js +0 -1
  42. package/tests/format/BasicIterator/BasicIterator.sol +0 -44
  43. package/tests/format/BasicIterator/__snapshots__/jsfmt.spec.js.snap +0 -102
  44. package/tests/format/BasicIterator/jsfmt.spec.js +0 -1
  45. package/tests/format/BinaryOperators/BinaryOperators.sol +0 -186
  46. package/tests/format/BinaryOperators/Parentheses.sol +0 -132
  47. package/tests/format/BinaryOperators/__snapshots__/jsfmt.spec.js.snap +0 -952
  48. package/tests/format/BinaryOperators/jsfmt.spec.js +0 -1
  49. package/tests/format/BreakingChangesV0.7.4/BreakingChangesV0.7.4.sol +0 -9
  50. package/tests/format/BreakingChangesV0.7.4/__snapshots__/jsfmt.spec.js.snap +0 -198
  51. package/tests/format/BreakingChangesV0.7.4/jsfmt.spec.js +0 -14
  52. package/tests/format/BreakingChangesV0.8.0/BreakingChangesV0.8.0.sol +0 -43
  53. package/tests/format/BreakingChangesV0.8.0/__snapshots__/jsfmt.spec.js.snap +0 -815
  54. package/tests/format/BreakingChangesV0.8.0/jsfmt.spec.js +0 -26
  55. package/tests/format/Comments/Comments.sol +0 -104
  56. package/tests/format/Comments/__snapshots__/jsfmt.spec.js.snap +0 -244
  57. package/tests/format/Comments/jsfmt.spec.js +0 -1
  58. package/tests/format/Conditional/Conditional.sol +0 -26
  59. package/tests/format/Conditional/__snapshots__/jsfmt.spec.js.snap +0 -80
  60. package/tests/format/Conditional/jsfmt.spec.js +0 -1
  61. package/tests/format/Constructors/Constructors.sol +0 -8
  62. package/tests/format/Constructors/__snapshots__/jsfmt.spec.js.snap +0 -54
  63. package/tests/format/Constructors/jsfmt.spec.js +0 -1
  64. package/tests/format/ContractDefinitions/ContractDefinitions.sol +0 -2
  65. package/tests/format/ContractDefinitions/__snapshots__/jsfmt.spec.js.snap +0 -22
  66. package/tests/format/ContractDefinitions/jsfmt.spec.js +0 -1
  67. package/tests/format/CustomErrors/CustomErrors.sol +0 -20
  68. package/tests/format/CustomErrors/__snapshots__/jsfmt.spec.js.snap +0 -50
  69. package/tests/format/CustomErrors/jsfmt.spec.js +0 -1
  70. package/tests/format/EnumDefinitions/EnumDefinitions.sol +0 -4
  71. package/tests/format/EnumDefinitions/__snapshots__/jsfmt.spec.js.snap +0 -72
  72. package/tests/format/EnumDefinitions/jsfmt.spec.js +0 -2
  73. package/tests/format/Etc/Etc.sol +0 -48
  74. package/tests/format/Etc/__snapshots__/jsfmt.spec.js.snap +0 -134
  75. package/tests/format/Etc/jsfmt.spec.js +0 -1
  76. package/tests/format/ExplicitVariableTypes/ExplicitVariableTypes.sol +0 -36
  77. package/tests/format/ExplicitVariableTypes/__snapshots__/jsfmt.spec.js.snap +0 -258
  78. package/tests/format/ExplicitVariableTypes/jsfmt.spec.js +0 -9
  79. package/tests/format/ForStatements/ForStatements.sol +0 -43
  80. package/tests/format/ForStatements/__snapshots__/jsfmt.spec.js.snap +0 -108
  81. package/tests/format/ForStatements/jsfmt.spec.js +0 -1
  82. package/tests/format/FunctionCalls/FunctionCalls.sol +0 -40
  83. package/tests/format/FunctionCalls/__snapshots__/jsfmt.spec.js.snap +0 -310
  84. package/tests/format/FunctionCalls/jsfmt.spec.js +0 -2
  85. package/tests/format/FunctionDefinitions/FunctionDefinitions.sol +0 -169
  86. package/tests/format/FunctionDefinitions/__snapshots__/jsfmt.spec.js.snap +0 -875
  87. package/tests/format/FunctionDefinitions/jsfmt.spec.js +0 -1
  88. package/tests/format/HexLiteral/HexLiteral.sol +0 -7
  89. package/tests/format/HexLiteral/__snapshots__/jsfmt.spec.js.snap +0 -57
  90. package/tests/format/HexLiteral/jsfmt.spec.js +0 -8
  91. package/tests/format/IfStatements/IfStatements.sol +0 -112
  92. package/tests/format/IfStatements/__snapshots__/jsfmt.spec.js.snap +0 -534
  93. package/tests/format/IfStatements/jsfmt.spec.js +0 -1
  94. package/tests/format/Immutable/Immutable.sol +0 -20
  95. package/tests/format/Immutable/__snapshots__/jsfmt.spec.js.snap +0 -53
  96. package/tests/format/Immutable/jsfmt.spec.js +0 -1
  97. package/tests/format/ImportDirective/ImportDirectives.sol +0 -5
  98. package/tests/format/ImportDirective/__snapshots__/jsfmt.spec.js.snap +0 -46
  99. package/tests/format/ImportDirective/jsfmt.spec.js +0 -2
  100. package/tests/format/Inbox/Inbox.sol +0 -33
  101. package/tests/format/Inbox/__snapshots__/jsfmt.spec.js.snap +0 -79
  102. package/tests/format/Inbox/jsfmt.spec.js +0 -1
  103. package/tests/format/IndexOf/IndexOf.sol +0 -67
  104. package/tests/format/IndexOf/__snapshots__/jsfmt.spec.js.snap +0 -144
  105. package/tests/format/IndexOf/jsfmt.spec.js +0 -1
  106. package/tests/format/IndexRangeAccess/IndexRangeAccess.sol +0 -9
  107. package/tests/format/IndexRangeAccess/__snapshots__/jsfmt.spec.js.snap +0 -31
  108. package/tests/format/IndexRangeAccess/jsfmt.spec.js +0 -1
  109. package/tests/format/InheritanceSpecifier/InheritanceSpecifier.sol +0 -2
  110. package/tests/format/InheritanceSpecifier/__snapshots__/jsfmt.spec.js.snap +0 -24
  111. package/tests/format/InheritanceSpecifier/jsfmt.spec.js +0 -1
  112. package/tests/format/Issues/Issue205.sol +0 -12
  113. package/tests/format/Issues/Issue289.sol +0 -6
  114. package/tests/format/Issues/Issue355.sol +0 -12
  115. package/tests/format/Issues/Issue385.sol +0 -13
  116. package/tests/format/Issues/Issue564.sol +0 -9
  117. package/tests/format/Issues/__snapshots__/jsfmt.spec.js.snap +0 -174
  118. package/tests/format/Issues/jsfmt.spec.js +0 -1
  119. package/tests/format/MemberAccess/MemberAccess.sol +0 -102
  120. package/tests/format/MemberAccess/__snapshots__/jsfmt.spec.js.snap +0 -243
  121. package/tests/format/MemberAccess/jsfmt.spec.js +0 -1
  122. package/tests/format/ModifierDefinitions/ModifierDefinitions.sol +0 -6
  123. package/tests/format/ModifierDefinitions/__snapshots__/jsfmt.spec.js.snap +0 -29
  124. package/tests/format/ModifierDefinitions/jsfmt.spec.js +0 -1
  125. package/tests/format/ModifierInvocations/ModifierInvocations.sol +0 -17
  126. package/tests/format/ModifierInvocations/__snapshots__/jsfmt.spec.js.snap +0 -51
  127. package/tests/format/ModifierInvocations/jsfmt.spec.js +0 -1
  128. package/tests/format/MultipartStrings/MultipartStrings.sol +0 -10
  129. package/tests/format/MultipartStrings/__snapshots__/jsfmt.spec.js.snap +0 -39
  130. package/tests/format/MultipartStrings/jsfmt.spec.js +0 -1
  131. package/tests/format/NameValueExpression/NameValueExpression.sol +0 -6
  132. package/tests/format/NameValueExpression/__snapshots__/jsfmt.spec.js.snap +0 -68
  133. package/tests/format/NameValueExpression/jsfmt.spec.js +0 -2
  134. package/tests/format/Ownable/Ownable.sol +0 -75
  135. package/tests/format/Ownable/__snapshots__/jsfmt.spec.js.snap +0 -163
  136. package/tests/format/Ownable/jsfmt.spec.js +0 -1
  137. package/tests/format/Parentheses/AddNoParentheses.sol +0 -92
  138. package/tests/format/Parentheses/BitAndNoParentheses.sol +0 -92
  139. package/tests/format/Parentheses/BitOrNoParentheses.sol +0 -92
  140. package/tests/format/Parentheses/BitXorNoParentheses.sol +0 -92
  141. package/tests/format/Parentheses/DivNoParentheses.sol +0 -92
  142. package/tests/format/Parentheses/ExpNoParentheses.sol +0 -92
  143. package/tests/format/Parentheses/LogicNoParentheses.sol +0 -20
  144. package/tests/format/Parentheses/ModNoParentheses.sol +0 -92
  145. package/tests/format/Parentheses/MulNoParentheses.sol +0 -92
  146. package/tests/format/Parentheses/ShiftLNoParentheses.sol +0 -92
  147. package/tests/format/Parentheses/ShiftRNoParentheses.sol +0 -92
  148. package/tests/format/Parentheses/SubNoParentheses.sol +0 -92
  149. package/tests/format/Parentheses/__snapshots__/jsfmt.spec.js.snap +0 -2237
  150. package/tests/format/Parentheses/jsfmt.spec.js +0 -1
  151. package/tests/format/Pragma/Pragma.sol +0 -8
  152. package/tests/format/Pragma/__snapshots__/jsfmt.spec.js.snap +0 -29
  153. package/tests/format/Pragma/jsfmt.spec.js +0 -1
  154. package/tests/format/PrettierIgnore/PrettierIgnore.sol +0 -39
  155. package/tests/format/PrettierIgnore/__snapshots__/jsfmt.spec.js.snap +0 -87
  156. package/tests/format/PrettierIgnore/jsfmt.spec.js +0 -1
  157. package/tests/format/Proxy/Proxy.sol +0 -52
  158. package/tests/format/Proxy/__snapshots__/jsfmt.spec.js.snap +0 -127
  159. package/tests/format/Proxy/jsfmt.spec.js +0 -1
  160. package/tests/format/RespectDefaultOptions/__snapshots__/jsfmt.spec.js.snap +0 -19
  161. package/tests/format/RespectDefaultOptions/jsfmt.spec.js +0 -5
  162. package/tests/format/RespectDefaultOptions/respect-default-options.js +0 -3
  163. package/tests/format/SampleCrowdsale/SampleCrowdsale.sol +0 -48
  164. package/tests/format/SampleCrowdsale/__snapshots__/jsfmt.spec.js.snap +0 -113
  165. package/tests/format/SampleCrowdsale/jsfmt.spec.js +0 -1
  166. package/tests/format/SimpleAuction/SimpleAuction.sol +0 -123
  167. package/tests/format/SimpleAuction/__snapshots__/jsfmt.spec.js.snap +0 -250
  168. package/tests/format/SimpleAuction/jsfmt.spec.js +0 -1
  169. package/tests/format/SimpleStorage/SimpleStorage.sol +0 -14
  170. package/tests/format/SimpleStorage/__snapshots__/jsfmt.spec.js.snap +0 -41
  171. package/tests/format/SimpleStorage/jsfmt.spec.js +0 -1
  172. package/tests/format/SplittableCommodity/SplittableCommodity.sol +0 -57
  173. package/tests/format/SplittableCommodity/__snapshots__/jsfmt.spec.js.snap +0 -135
  174. package/tests/format/SplittableCommodity/jsfmt.spec.js +0 -1
  175. package/tests/format/StateVariableDeclarations/StateVariableDeclarations.sol +0 -3
  176. package/tests/format/StateVariableDeclarations/__snapshots__/jsfmt.spec.js.snap +0 -20
  177. package/tests/format/StateVariableDeclarations/jsfmt.spec.js +0 -1
  178. package/tests/format/StringLiteral/StringLiteral.sol +0 -11
  179. package/tests/format/StringLiteral/__snapshots__/jsfmt.spec.js.snap +0 -36
  180. package/tests/format/StringLiteral/jsfmt.spec.js +0 -1
  181. package/tests/format/StyleGuide/BlankLines.sol +0 -19
  182. package/tests/format/StyleGuide/ControlStructures.sol +0 -57
  183. package/tests/format/StyleGuide/FunctionDeclaration.sol +0 -135
  184. package/tests/format/StyleGuide/Mappings.sol +0 -9
  185. package/tests/format/StyleGuide/MaximumLineLength.sol +0 -67
  186. package/tests/format/StyleGuide/OtherRecommendations.sol +0 -29
  187. package/tests/format/StyleGuide/VariableDeclarations.sol +0 -6
  188. package/tests/format/StyleGuide/WhitespaceInExpressions.sol +0 -13
  189. package/tests/format/StyleGuide/__snapshots__/jsfmt.spec.js.snap +0 -772
  190. package/tests/format/StyleGuide/jsfmt.spec.js +0 -1
  191. package/tests/format/TryCatch/TryCatch.sol +0 -103
  192. package/tests/format/TryCatch/__snapshots__/jsfmt.spec.js.snap +0 -241
  193. package/tests/format/TryCatch/jsfmt.spec.js +0 -1
  194. package/tests/format/Tupples/Tupples.sol +0 -25
  195. package/tests/format/Tupples/__snapshots__/jsfmt.spec.js.snap +0 -74
  196. package/tests/format/Tupples/jsfmt.spec.js +0 -1
  197. package/tests/format/TypeDefinition/TypeDefinition.sol +0 -12
  198. package/tests/format/TypeDefinition/__snapshots__/jsfmt.spec.js.snap +0 -74
  199. package/tests/format/TypeDefinition/jsfmt.spec.js +0 -1
  200. package/tests/format/WhileStatements/WhileStatements.sol +0 -37
  201. package/tests/format/WhileStatements/__snapshots__/jsfmt.spec.js.snap +0 -110
  202. package/tests/format/WhileStatements/jsfmt.spec.js +0 -1
  203. package/tests/format/WrongCompiler/WrongCompiler.sol +0 -3
  204. package/tests/format/WrongCompiler/__snapshots__/jsfmt.spec.js.snap +0 -55
  205. package/tests/format/WrongCompiler/jsfmt.spec.js +0 -10
  206. package/tests/format/quotes/Quotes.sol +0 -24
  207. package/tests/format/quotes/__snapshots__/jsfmt.spec.js.snap +0 -135
  208. package/tests/format/quotes/jsfmt.spec.js +0 -2
  209. package/tests/format/strings/__snapshots__/jsfmt.spec.js.snap +0 -1579
  210. package/tests/format/strings/jsfmt.spec.js +0 -1
  211. package/tests/format/strings/strings.sol +0 -717
  212. package/tests/unit/binary-operator-printers/__snapshots__/index.test.js.snap +0 -13
  213. package/tests/unit/binary-operator-printers/index.test.js +0 -5
  214. package/tests/unit/comments/printer.test.js +0 -9
  215. package/tests/unit/prettier-version.test.js +0 -26
  216. package/tests/unit/printer.test.js +0 -9
@@ -1,1579 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`strings.sol format 1`] = `
4
- ====================================options=====================================
5
- parsers: ["solidity-parse"]
6
- printWidth: 80
7
- | printWidth
8
- =====================================input======================================
9
- /*
10
- * @title String & slice utility library for Solidity contracts.
11
- * @author Nick Johnson <arachnid@notdot.net>
12
- *
13
- * @dev Functionality in this library is largely implemented using an
14
- * abstraction called a 'slice'. A slice represents a part of a string -
15
- * anything from the entire string to a single character, or even no
16
- * characters at all (a 0-length slice). Since a slice only has to specify
17
- * an offset and a length, copying and manipulating slices is a lot less
18
- * expensive than copying and manipulating the strings they reference.
19
- *
20
- * To further reduce gas costs, most functions on slice that need to return
21
- * a slice modify the original one instead of allocating a new one; for
22
- * instance, \`s.split(".")\` will return the text up to the first '.',
23
- * modifying s to only contain the remainder of the string after the '.'.
24
- * In situations where you do not want to modify the original slice, you
25
- * can make a copy first with \`.copy()\`, for example:
26
- * \`s.copy().split(".")\`. Try and avoid using this idiom in loops; since
27
- * Solidity has no memory management, it will result in allocating many
28
- * short-lived slices that are later discarded.
29
- *
30
- * Functions that return two slices come in two versions: a non-allocating
31
- * version that takes the second slice as an argument, modifying it in
32
- * place, and an allocating version that allocates and returns the second
33
- * slice; see \`nextRune\` for example.
34
- *
35
- * Functions that have to copy string data will return strings rather than
36
- * slices; these can be cast back to slices for further processing if
37
- * required.
38
- *
39
- * For convenience, some functions are provided with non-modifying
40
- * variants that create a new slice and return both; for instance,
41
- * \`s.splitNew('.')\` leaves s unmodified, and returns two values
42
- * corresponding to the left and right parts of the string.
43
- */
44
-
45
- pragma solidity ^0.4.14;
46
-
47
- library strings {
48
- struct slice {
49
- uint _len;
50
- uint _ptr;
51
- }
52
-
53
- function memcpy(uint dest, uint src, uint len) private pure {
54
- // Copy word-length chunks while possible
55
- for(; len >= 32; len -= 32) {
56
- assembly {
57
- mstore(dest, mload(src))
58
- }
59
- dest += 32;
60
- src += 32;
61
- }
62
-
63
- // Copy remaining bytes
64
- uint mask = 256 ** (32 - len) - 1;
65
- assembly {
66
- let srcpart := and(mload(src), not(mask))
67
- let destpart := and(mload(dest), mask)
68
- mstore(dest, or(destpart, srcpart))
69
- }
70
- }
71
-
72
- /*
73
- * @dev Returns a slice containing the entire string.
74
- * @param self The string to make a slice from.
75
- * @return A newly allocated slice containing the entire string.
76
- */
77
- function toSlice(string memory self) internal pure returns (slice memory) {
78
- uint ptr;
79
- assembly {
80
- ptr := add(self, 0x20)
81
- }
82
- return slice(bytes(self).length, ptr);
83
- }
84
-
85
- /*
86
- * @dev Returns the length of a null-terminated bytes32 string.
87
- * @param self The value to find the length of.
88
- * @return The length of the string, from 0 to 32.
89
- */
90
- function len(bytes32 self) internal pure returns (uint) {
91
- uint ret;
92
- if (self == 0)
93
- return 0;
94
- if (self & 0xffffffffffffffffffffffffffffffff == 0) {
95
- ret += 16;
96
- self = bytes32(uint(self) / 0x100000000000000000000000000000000);
97
- }
98
- if (self & 0xffffffffffffffff == 0) {
99
- ret += 8;
100
- self = bytes32(uint(self) / 0x10000000000000000);
101
- }
102
- if (self & 0xffffffff == 0) {
103
- ret += 4;
104
- self = bytes32(uint(self) / 0x100000000);
105
- }
106
- if (self & 0xffff == 0) {
107
- ret += 2;
108
- self = bytes32(uint(self) / 0x10000);
109
- }
110
- if (self & 0xff == 0) {
111
- ret += 1;
112
- }
113
- return 32 - ret;
114
- }
115
-
116
- /*
117
- * @dev Returns a slice containing the entire bytes32, interpreted as a
118
- * null-terminated utf-8 string.
119
- * @param self The bytes32 value to convert to a slice.
120
- * @return A new slice containing the value of the input argument up to the
121
- * first null.
122
- */
123
- function toSliceB32(bytes32 self) internal pure returns (slice memory ret) {
124
- // Allocate space for \`self\` in memory, copy it there, and point ret at it
125
- assembly {
126
- let ptr := mload(0x40)
127
- mstore(0x40, add(ptr, 0x20))
128
- mstore(ptr, self)
129
- mstore(add(ret, 0x20), ptr)
130
- }
131
- ret._len = len(self);
132
- }
133
-
134
- /*
135
- * @dev Returns a new slice containing the same data as the current slice.
136
- * @param self The slice to copy.
137
- * @return A new slice containing the same data as \`self\`.
138
- */
139
- function copy(slice memory self) internal pure returns (slice memory) {
140
- return slice(self._len, self._ptr);
141
- }
142
-
143
- /*
144
- * @dev Copies a slice to a new string.
145
- * @param self The slice to copy.
146
- * @return A newly allocated string containing the slice's text.
147
- */
148
- function toString(slice memory self) internal pure returns (string memory) {
149
- string memory ret = new string(self._len);
150
- uint retptr;
151
- assembly { retptr := add(ret, 32) }
152
-
153
- memcpy(retptr, self._ptr, self._len);
154
- return ret;
155
- }
156
-
157
- /*
158
- * @dev Returns the length in runes of the slice. Note that this operation
159
- * takes time proportional to the length of the slice; avoid using it
160
- * in loops, and call \`slice.empty()\` if you only need to know whether
161
- * the slice is empty or not.
162
- * @param self The slice to operate on.
163
- * @return The length of the slice in runes.
164
- */
165
- function len(slice memory self) internal pure returns (uint l) {
166
- // Starting at ptr-31 means the LSB will be the byte we care about
167
- uint ptr = self._ptr - 31;
168
- uint end = ptr + self._len;
169
- for (l = 0; ptr < end; l++) {
170
- uint8 b;
171
- assembly { b := and(mload(ptr), 0xFF) }
172
- if (b < 0x80) {
173
- ptr += 1;
174
- } else if(b < 0xE0) {
175
- ptr += 2;
176
- } else if(b < 0xF0) {
177
- ptr += 3;
178
- } else if(b < 0xF8) {
179
- ptr += 4;
180
- } else if(b < 0xFC) {
181
- ptr += 5;
182
- } else {
183
- ptr += 6;
184
- }
185
- }
186
- }
187
-
188
- /*
189
- * @dev Returns true if the slice is empty (has a length of 0).
190
- * @param self The slice to operate on.
191
- * @return True if the slice is empty, False otherwise.
192
- */
193
- function empty(slice memory self) internal pure returns (bool) {
194
- return self._len == 0;
195
- }
196
-
197
- /*
198
- * @dev Returns a positive number if \`other\` comes lexicographically after
199
- * \`self\`, a negative number if it comes before, or zero if the
200
- * contents of the two slices are equal. Comparison is done per-rune,
201
- * on unicode codepoints.
202
- * @param self The first slice to compare.
203
- * @param other The second slice to compare.
204
- * @return The result of the comparison.
205
- */
206
- function compare(slice memory self, slice memory other) internal pure returns (int) {
207
- uint shortest = self._len;
208
- if (other._len < self._len)
209
- shortest = other._len;
210
-
211
- uint selfptr = self._ptr;
212
- uint otherptr = other._ptr;
213
- for (uint idx = 0; idx < shortest; idx += 32) {
214
- uint a;
215
- uint b;
216
- assembly {
217
- a := mload(selfptr)
218
- b := mload(otherptr)
219
- }
220
- if (a != b) {
221
- // Mask out irrelevant bytes and check again
222
- uint256 mask = uint256(-1); // 0xffff...
223
- if(shortest < 32) {
224
- mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
225
- }
226
- uint256 diff = (a & mask) - (b & mask);
227
- if (diff != 0)
228
- return int(diff);
229
- }
230
- selfptr += 32;
231
- otherptr += 32;
232
- }
233
- return int(self._len) - int(other._len);
234
- }
235
-
236
- /*
237
- * @dev Returns true if the two slices contain the same text.
238
- * @param self The first slice to compare.
239
- * @param self The second slice to compare.
240
- * @return True if the slices are equal, false otherwise.
241
- */
242
- function equals(slice memory self, slice memory other) internal pure returns (bool) {
243
- return compare(self, other) == 0;
244
- }
245
-
246
- /*
247
- * @dev Extracts the first rune in the slice into \`rune\`, advancing the
248
- * slice to point to the next rune and returning \`self\`.
249
- * @param self The slice to operate on.
250
- * @param rune The slice that will contain the first rune.
251
- * @return \`rune\`.
252
- */
253
- function nextRune(slice memory self, slice memory rune) internal pure returns (slice memory) {
254
- rune._ptr = self._ptr;
255
-
256
- if (self._len == 0) {
257
- rune._len = 0;
258
- return rune;
259
- }
260
-
261
- uint l;
262
- uint b;
263
- // Load the first byte of the rune into the LSBs of b
264
- assembly { b := and(mload(sub(mload(add(self, 32)), 31)), 0xFF) }
265
- if (b < 0x80) {
266
- l = 1;
267
- } else if(b < 0xE0) {
268
- l = 2;
269
- } else if(b < 0xF0) {
270
- l = 3;
271
- } else {
272
- l = 4;
273
- }
274
-
275
- // Check for truncated codepoints
276
- if (l > self._len) {
277
- rune._len = self._len;
278
- self._ptr += self._len;
279
- self._len = 0;
280
- return rune;
281
- }
282
-
283
- self._ptr += l;
284
- self._len -= l;
285
- rune._len = l;
286
- return rune;
287
- }
288
-
289
- /*
290
- * @dev Returns the first rune in the slice, advancing the slice to point
291
- * to the next rune.
292
- * @param self The slice to operate on.
293
- * @return A slice containing only the first rune from \`self\`.
294
- */
295
- function nextRune(slice memory self) internal pure returns (slice memory ret) {
296
- nextRune(self, ret);
297
- }
298
-
299
- /*
300
- * @dev Returns the number of the first codepoint in the slice.
301
- * @param self The slice to operate on.
302
- * @return The number of the first codepoint in the slice.
303
- */
304
- function ord(slice memory self) internal pure returns (uint ret) {
305
- if (self._len == 0) {
306
- return 0;
307
- }
308
-
309
- uint word;
310
- uint length;
311
- uint divisor = 2 ** 248;
312
-
313
- // Load the rune into the MSBs of b
314
- assembly { word:= mload(mload(add(self, 32))) }
315
- uint b = word / divisor;
316
- if (b < 0x80) {
317
- ret = b;
318
- length = 1;
319
- } else if(b < 0xE0) {
320
- ret = b & 0x1F;
321
- length = 2;
322
- } else if(b < 0xF0) {
323
- ret = b & 0x0F;
324
- length = 3;
325
- } else {
326
- ret = b & 0x07;
327
- length = 4;
328
- }
329
-
330
- // Check for truncated codepoints
331
- if (length > self._len) {
332
- return 0;
333
- }
334
-
335
- for (uint i = 1; i < length; i++) {
336
- divisor = divisor / 256;
337
- b = (word / divisor) & 0xFF;
338
- if (b & 0xC0 != 0x80) {
339
- // Invalid UTF-8 sequence
340
- return 0;
341
- }
342
- ret = (ret * 64) | (b & 0x3F);
343
- }
344
-
345
- return ret;
346
- }
347
-
348
- /*
349
- * @dev Returns the keccak-256 hash of the slice.
350
- * @param self The slice to hash.
351
- * @return The hash of the slice.
352
- */
353
- function keccak(slice memory self) internal pure returns (bytes32 ret) {
354
- assembly {
355
- ret := keccak256(mload(add(self, 32)), mload(self))
356
- }
357
- }
358
-
359
- /*
360
- * @dev Returns true if \`self\` starts with \`needle\`.
361
- * @param self The slice to operate on.
362
- * @param needle The slice to search for.
363
- * @return True if the slice starts with the provided text, false otherwise.
364
- */
365
- function startsWith(slice memory self, slice memory needle) internal pure returns (bool) {
366
- if (self._len < needle._len) {
367
- return false;
368
- }
369
-
370
- if (self._ptr == needle._ptr) {
371
- return true;
372
- }
373
-
374
- bool equal;
375
- assembly {
376
- let length := mload(needle)
377
- let selfptr := mload(add(self, 0x20))
378
- let needleptr := mload(add(needle, 0x20))
379
- equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
380
- }
381
- return equal;
382
- }
383
-
384
- /*
385
- * @dev If \`self\` starts with \`needle\`, \`needle\` is removed from the
386
- * beginning of \`self\`. Otherwise, \`self\` is unmodified.
387
- * @param self The slice to operate on.
388
- * @param needle The slice to search for.
389
- * @return \`self\`
390
- */
391
- function beyond(slice memory self, slice memory needle) internal pure returns (slice memory) {
392
- if (self._len < needle._len) {
393
- return self;
394
- }
395
-
396
- bool equal = true;
397
- if (self._ptr != needle._ptr) {
398
- assembly {
399
- let length := mload(needle)
400
- let selfptr := mload(add(self, 0x20))
401
- let needleptr := mload(add(needle, 0x20))
402
- equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
403
- }
404
- }
405
-
406
- if (equal) {
407
- self._len -= needle._len;
408
- self._ptr += needle._len;
409
- }
410
-
411
- return self;
412
- }
413
-
414
- /*
415
- * @dev Returns true if the slice ends with \`needle\`.
416
- * @param self The slice to operate on.
417
- * @param needle The slice to search for.
418
- * @return True if the slice starts with the provided text, false otherwise.
419
- */
420
- function endsWith(slice memory self, slice memory needle) internal pure returns (bool) {
421
- if (self._len < needle._len) {
422
- return false;
423
- }
424
-
425
- uint selfptr = self._ptr + self._len - needle._len;
426
-
427
- if (selfptr == needle._ptr) {
428
- return true;
429
- }
430
-
431
- bool equal;
432
- assembly {
433
- let length := mload(needle)
434
- let needleptr := mload(add(needle, 0x20))
435
- equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
436
- }
437
-
438
- return equal;
439
- }
440
-
441
- /*
442
- * @dev If \`self\` ends with \`needle\`, \`needle\` is removed from the
443
- * end of \`self\`. Otherwise, \`self\` is unmodified.
444
- * @param self The slice to operate on.
445
- * @param needle The slice to search for.
446
- * @return \`self\`
447
- */
448
- function until(slice memory self, slice memory needle) internal pure returns (slice memory) {
449
- if (self._len < needle._len) {
450
- return self;
451
- }
452
-
453
- uint selfptr = self._ptr + self._len - needle._len;
454
- bool equal = true;
455
- if (selfptr != needle._ptr) {
456
- assembly {
457
- let length := mload(needle)
458
- let needleptr := mload(add(needle, 0x20))
459
- equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
460
- }
461
- }
462
-
463
- if (equal) {
464
- self._len -= needle._len;
465
- }
466
-
467
- return self;
468
- }
469
-
470
- // Returns the memory address of the first byte of the first occurrence of
471
- // \`needle\` in \`self\`, or the first byte after \`self\` if not found.
472
- function findPtr(uint selflen, uint selfptr, uint needlelen, uint needleptr) private pure returns (uint) {
473
- uint ptr = selfptr;
474
- uint idx;
475
-
476
- if (needlelen <= selflen) {
477
- if (needlelen <= 32) {
478
- bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
479
-
480
- bytes32 needledata;
481
- assembly { needledata := and(mload(needleptr), mask) }
482
-
483
- uint end = selfptr + selflen - needlelen;
484
- bytes32 ptrdata;
485
- assembly { ptrdata := and(mload(ptr), mask) }
486
-
487
- while (ptrdata != needledata) {
488
- if (ptr >= end)
489
- return selfptr + selflen;
490
- ptr++;
491
- assembly { ptrdata := and(mload(ptr), mask) }
492
- }
493
- return ptr;
494
- } else {
495
- // For long needles, use hashing
496
- bytes32 hash;
497
- assembly { hash := keccak256(needleptr, needlelen) }
498
-
499
- for (idx = 0; idx <= selflen - needlelen; idx++) {
500
- bytes32 testHash;
501
- assembly { testHash := keccak256(ptr, needlelen) }
502
- if (hash == testHash)
503
- return ptr;
504
- ptr += 1;
505
- }
506
- }
507
- }
508
- return selfptr + selflen;
509
- }
510
-
511
- // Returns the memory address of the first byte after the last occurrence of
512
- // \`needle\` in \`self\`, or the address of \`self\` if not found.
513
- function rfindPtr(uint selflen, uint selfptr, uint needlelen, uint needleptr) private pure returns (uint) {
514
- uint ptr;
515
-
516
- if (needlelen <= selflen) {
517
- if (needlelen <= 32) {
518
- bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
519
-
520
- bytes32 needledata;
521
- assembly { needledata := and(mload(needleptr), mask) }
522
-
523
- ptr = selfptr + selflen - needlelen;
524
- bytes32 ptrdata;
525
- assembly { ptrdata := and(mload(ptr), mask) }
526
-
527
- while (ptrdata != needledata) {
528
- if (ptr <= selfptr)
529
- return selfptr;
530
- ptr--;
531
- assembly { ptrdata := and(mload(ptr), mask) }
532
- }
533
- return ptr + needlelen;
534
- } else {
535
- // For long needles, use hashing
536
- bytes32 hash;
537
- assembly { hash := keccak256(needleptr, needlelen) }
538
- ptr = selfptr + (selflen - needlelen);
539
- while (ptr >= selfptr) {
540
- bytes32 testHash;
541
- assembly { testHash := keccak256(ptr, needlelen) }
542
- if (hash == testHash)
543
- return ptr + needlelen;
544
- ptr -= 1;
545
- }
546
- }
547
- }
548
- return selfptr;
549
- }
550
-
551
- /*
552
- * @dev Modifies \`self\` to contain everything from the first occurrence of
553
- * \`needle\` to the end of the slice. \`self\` is set to the empty slice
554
- * if \`needle\` is not found.
555
- * @param self The slice to search and modify.
556
- * @param needle The text to search for.
557
- * @return \`self\`.
558
- */
559
- function find(slice memory self, slice memory needle) internal pure returns (slice memory) {
560
- uint ptr = findPtr(self._len, self._ptr, needle._len, needle._ptr);
561
- self._len -= ptr - self._ptr;
562
- self._ptr = ptr;
563
- return self;
564
- }
565
-
566
- /*
567
- * @dev Modifies \`self\` to contain the part of the string from the start of
568
- * \`self\` to the end of the first occurrence of \`needle\`. If \`needle\`
569
- * is not found, \`self\` is set to the empty slice.
570
- * @param self The slice to search and modify.
571
- * @param needle The text to search for.
572
- * @return \`self\`.
573
- */
574
- function rfind(slice memory self, slice memory needle) internal pure returns (slice memory) {
575
- uint ptr = rfindPtr(self._len, self._ptr, needle._len, needle._ptr);
576
- self._len = ptr - self._ptr;
577
- return self;
578
- }
579
-
580
- /*
581
- * @dev Splits the slice, setting \`self\` to everything after the first
582
- * occurrence of \`needle\`, and \`token\` to everything before it. If
583
- * \`needle\` does not occur in \`self\`, \`self\` is set to the empty slice,
584
- * and \`token\` is set to the entirety of \`self\`.
585
- * @param self The slice to split.
586
- * @param needle The text to search for in \`self\`.
587
- * @param token An output parameter to which the first token is written.
588
- * @return \`token\`.
589
- */
590
- function split(slice memory self, slice memory needle, slice memory token) internal pure returns (slice memory) {
591
- uint ptr = findPtr(self._len, self._ptr, needle._len, needle._ptr);
592
- token._ptr = self._ptr;
593
- token._len = ptr - self._ptr;
594
- if (ptr == self._ptr + self._len) {
595
- // Not found
596
- self._len = 0;
597
- } else {
598
- self._len -= token._len + needle._len;
599
- self._ptr = ptr + needle._len;
600
- }
601
- return token;
602
- }
603
-
604
- /*
605
- * @dev Splits the slice, setting \`self\` to everything after the first
606
- * occurrence of \`needle\`, and returning everything before it. If
607
- * \`needle\` does not occur in \`self\`, \`self\` is set to the empty slice,
608
- * and the entirety of \`self\` is returned.
609
- * @param self The slice to split.
610
- * @param needle The text to search for in \`self\`.
611
- * @return The part of \`self\` up to the first occurrence of \`delim\`.
612
- */
613
- function split(slice memory self, slice memory needle) internal pure returns (slice memory token) {
614
- split(self, needle, token);
615
- }
616
-
617
- /*
618
- * @dev Splits the slice, setting \`self\` to everything before the last
619
- * occurrence of \`needle\`, and \`token\` to everything after it. If
620
- * \`needle\` does not occur in \`self\`, \`self\` is set to the empty slice,
621
- * and \`token\` is set to the entirety of \`self\`.
622
- * @param self The slice to split.
623
- * @param needle The text to search for in \`self\`.
624
- * @param token An output parameter to which the first token is written.
625
- * @return \`token\`.
626
- */
627
- function rsplit(slice memory self, slice memory needle, slice memory token) internal pure returns (slice memory) {
628
- uint ptr = rfindPtr(self._len, self._ptr, needle._len, needle._ptr);
629
- token._ptr = ptr;
630
- token._len = self._len - (ptr - self._ptr);
631
- if (ptr == self._ptr) {
632
- // Not found
633
- self._len = 0;
634
- } else {
635
- self._len -= token._len + needle._len;
636
- }
637
- return token;
638
- }
639
-
640
- /*
641
- * @dev Splits the slice, setting \`self\` to everything before the last
642
- * occurrence of \`needle\`, and returning everything after it. If
643
- * \`needle\` does not occur in \`self\`, \`self\` is set to the empty slice,
644
- * and the entirety of \`self\` is returned.
645
- * @param self The slice to split.
646
- * @param needle The text to search for in \`self\`.
647
- * @return The part of \`self\` after the last occurrence of \`delim\`.
648
- */
649
- function rsplit(slice memory self, slice memory needle) internal pure returns (slice memory token) {
650
- rsplit(self, needle, token);
651
- }
652
-
653
- /*
654
- * @dev Counts the number of nonoverlapping occurrences of \`needle\` in \`self\`.
655
- * @param self The slice to search.
656
- * @param needle The text to search for in \`self\`.
657
- * @return The number of occurrences of \`needle\` found in \`self\`.
658
- */
659
- function count(slice memory self, slice memory needle) internal pure returns (uint cnt) {
660
- uint ptr = findPtr(self._len, self._ptr, needle._len, needle._ptr) + needle._len;
661
- while (ptr <= self._ptr + self._len) {
662
- cnt++;
663
- ptr = findPtr(self._len - (ptr - self._ptr), ptr, needle._len, needle._ptr) + needle._len;
664
- }
665
- }
666
-
667
- /*
668
- * @dev Returns True if \`self\` contains \`needle\`.
669
- * @param self The slice to search.
670
- * @param needle The text to search for in \`self\`.
671
- * @return True if \`needle\` is found in \`self\`, false otherwise.
672
- */
673
- function contains(slice memory self, slice memory needle) internal pure returns (bool) {
674
- return rfindPtr(self._len, self._ptr, needle._len, needle._ptr) != self._ptr;
675
- }
676
-
677
- /*
678
- * @dev Returns a newly allocated string containing the concatenation of
679
- * \`self\` and \`other\`.
680
- * @param self The first slice to concatenate.
681
- * @param other The second slice to concatenate.
682
- * @return The concatenation of the two strings.
683
- */
684
- function concat(slice memory self, slice memory other) internal pure returns (string memory) {
685
- string memory ret = new string(self._len + other._len);
686
- uint retptr;
687
- assembly { retptr := add(ret, 32) }
688
- memcpy(retptr, self._ptr, self._len);
689
- memcpy(retptr + self._len, other._ptr, other._len);
690
- return ret;
691
- }
692
-
693
- /*
694
- * @dev Joins an array of slices, using \`self\` as a delimiter, returning a
695
- * newly allocated string.
696
- * @param self The delimiter to use.
697
- * @param parts A list of slices to join.
698
- * @return A newly allocated string containing all the slices in \`parts\`,
699
- * joined with \`self\`.
700
- */
701
- function join(slice memory self, slice[] memory parts) internal pure returns (string memory) {
702
- if (parts.length == 0)
703
- return "";
704
-
705
- uint length = self._len * (parts.length - 1);
706
- for(uint i = 0; i < parts.length; i++) {
707
- length += parts[i]._len;
708
- }
709
-
710
- string memory ret = new string(length);
711
- uint retptr;
712
- assembly { retptr := add(ret, 32) }
713
-
714
- for(i = 0; i < parts.length; i++) {
715
- memcpy(retptr, parts[i]._ptr, parts[i]._len);
716
- retptr += parts[i]._len;
717
- if (i < parts.length - 1) {
718
- memcpy(retptr, self._ptr, self._len);
719
- retptr += self._len;
720
- }
721
- }
722
-
723
- return ret;
724
- }
725
- }
726
-
727
- =====================================output=====================================
728
- /*
729
- * @title String & slice utility library for Solidity contracts.
730
- * @author Nick Johnson <arachnid@notdot.net>
731
- *
732
- * @dev Functionality in this library is largely implemented using an
733
- * abstraction called a 'slice'. A slice represents a part of a string -
734
- * anything from the entire string to a single character, or even no
735
- * characters at all (a 0-length slice). Since a slice only has to specify
736
- * an offset and a length, copying and manipulating slices is a lot less
737
- * expensive than copying and manipulating the strings they reference.
738
- *
739
- * To further reduce gas costs, most functions on slice that need to return
740
- * a slice modify the original one instead of allocating a new one; for
741
- * instance, \`s.split(".")\` will return the text up to the first '.',
742
- * modifying s to only contain the remainder of the string after the '.'.
743
- * In situations where you do not want to modify the original slice, you
744
- * can make a copy first with \`.copy()\`, for example:
745
- * \`s.copy().split(".")\`. Try and avoid using this idiom in loops; since
746
- * Solidity has no memory management, it will result in allocating many
747
- * short-lived slices that are later discarded.
748
- *
749
- * Functions that return two slices come in two versions: a non-allocating
750
- * version that takes the second slice as an argument, modifying it in
751
- * place, and an allocating version that allocates and returns the second
752
- * slice; see \`nextRune\` for example.
753
- *
754
- * Functions that have to copy string data will return strings rather than
755
- * slices; these can be cast back to slices for further processing if
756
- * required.
757
- *
758
- * For convenience, some functions are provided with non-modifying
759
- * variants that create a new slice and return both; for instance,
760
- * \`s.splitNew('.')\` leaves s unmodified, and returns two values
761
- * corresponding to the left and right parts of the string.
762
- */
763
-
764
- pragma solidity ^0.4.14;
765
-
766
- library strings {
767
- struct slice {
768
- uint256 _len;
769
- uint256 _ptr;
770
- }
771
-
772
- function memcpy(
773
- uint256 dest,
774
- uint256 src,
775
- uint256 len
776
- ) private pure {
777
- // Copy word-length chunks while possible
778
- for (; len >= 32; len -= 32) {
779
- assembly {
780
- mstore(dest, mload(src))
781
- }
782
- dest += 32;
783
- src += 32;
784
- }
785
-
786
- // Copy remaining bytes
787
- uint256 mask = 256**(32 - len) - 1;
788
- assembly {
789
- let srcpart := and(mload(src), not(mask))
790
- let destpart := and(mload(dest), mask)
791
- mstore(dest, or(destpart, srcpart))
792
- }
793
- }
794
-
795
- /*
796
- * @dev Returns a slice containing the entire string.
797
- * @param self The string to make a slice from.
798
- * @return A newly allocated slice containing the entire string.
799
- */
800
- function toSlice(string memory self) internal pure returns (slice memory) {
801
- uint256 ptr;
802
- assembly {
803
- ptr := add(self, 0x20)
804
- }
805
- return slice(bytes(self).length, ptr);
806
- }
807
-
808
- /*
809
- * @dev Returns the length of a null-terminated bytes32 string.
810
- * @param self The value to find the length of.
811
- * @return The length of the string, from 0 to 32.
812
- */
813
- function len(bytes32 self) internal pure returns (uint256) {
814
- uint256 ret;
815
- if (self == 0) return 0;
816
- if (self & 0xffffffffffffffffffffffffffffffff == 0) {
817
- ret += 16;
818
- self = bytes32(uint256(self) / 0x100000000000000000000000000000000);
819
- }
820
- if (self & 0xffffffffffffffff == 0) {
821
- ret += 8;
822
- self = bytes32(uint256(self) / 0x10000000000000000);
823
- }
824
- if (self & 0xffffffff == 0) {
825
- ret += 4;
826
- self = bytes32(uint256(self) / 0x100000000);
827
- }
828
- if (self & 0xffff == 0) {
829
- ret += 2;
830
- self = bytes32(uint256(self) / 0x10000);
831
- }
832
- if (self & 0xff == 0) {
833
- ret += 1;
834
- }
835
- return 32 - ret;
836
- }
837
-
838
- /*
839
- * @dev Returns a slice containing the entire bytes32, interpreted as a
840
- * null-terminated utf-8 string.
841
- * @param self The bytes32 value to convert to a slice.
842
- * @return A new slice containing the value of the input argument up to the
843
- * first null.
844
- */
845
- function toSliceB32(bytes32 self) internal pure returns (slice memory ret) {
846
- // Allocate space for \`self\` in memory, copy it there, and point ret at it
847
- assembly {
848
- let ptr := mload(0x40)
849
- mstore(0x40, add(ptr, 0x20))
850
- mstore(ptr, self)
851
- mstore(add(ret, 0x20), ptr)
852
- }
853
- ret._len = len(self);
854
- }
855
-
856
- /*
857
- * @dev Returns a new slice containing the same data as the current slice.
858
- * @param self The slice to copy.
859
- * @return A new slice containing the same data as \`self\`.
860
- */
861
- function copy(slice memory self) internal pure returns (slice memory) {
862
- return slice(self._len, self._ptr);
863
- }
864
-
865
- /*
866
- * @dev Copies a slice to a new string.
867
- * @param self The slice to copy.
868
- * @return A newly allocated string containing the slice's text.
869
- */
870
- function toString(slice memory self) internal pure returns (string memory) {
871
- string memory ret = new string(self._len);
872
- uint256 retptr;
873
- assembly {
874
- retptr := add(ret, 32)
875
- }
876
-
877
- memcpy(retptr, self._ptr, self._len);
878
- return ret;
879
- }
880
-
881
- /*
882
- * @dev Returns the length in runes of the slice. Note that this operation
883
- * takes time proportional to the length of the slice; avoid using it
884
- * in loops, and call \`slice.empty()\` if you only need to know whether
885
- * the slice is empty or not.
886
- * @param self The slice to operate on.
887
- * @return The length of the slice in runes.
888
- */
889
- function len(slice memory self) internal pure returns (uint256 l) {
890
- // Starting at ptr-31 means the LSB will be the byte we care about
891
- uint256 ptr = self._ptr - 31;
892
- uint256 end = ptr + self._len;
893
- for (l = 0; ptr < end; l++) {
894
- uint8 b;
895
- assembly {
896
- b := and(mload(ptr), 0xFF)
897
- }
898
- if (b < 0x80) {
899
- ptr += 1;
900
- } else if (b < 0xE0) {
901
- ptr += 2;
902
- } else if (b < 0xF0) {
903
- ptr += 3;
904
- } else if (b < 0xF8) {
905
- ptr += 4;
906
- } else if (b < 0xFC) {
907
- ptr += 5;
908
- } else {
909
- ptr += 6;
910
- }
911
- }
912
- }
913
-
914
- /*
915
- * @dev Returns true if the slice is empty (has a length of 0).
916
- * @param self The slice to operate on.
917
- * @return True if the slice is empty, False otherwise.
918
- */
919
- function empty(slice memory self) internal pure returns (bool) {
920
- return self._len == 0;
921
- }
922
-
923
- /*
924
- * @dev Returns a positive number if \`other\` comes lexicographically after
925
- * \`self\`, a negative number if it comes before, or zero if the
926
- * contents of the two slices are equal. Comparison is done per-rune,
927
- * on unicode codepoints.
928
- * @param self The first slice to compare.
929
- * @param other The second slice to compare.
930
- * @return The result of the comparison.
931
- */
932
- function compare(slice memory self, slice memory other)
933
- internal
934
- pure
935
- returns (int256)
936
- {
937
- uint256 shortest = self._len;
938
- if (other._len < self._len) shortest = other._len;
939
-
940
- uint256 selfptr = self._ptr;
941
- uint256 otherptr = other._ptr;
942
- for (uint256 idx = 0; idx < shortest; idx += 32) {
943
- uint256 a;
944
- uint256 b;
945
- assembly {
946
- a := mload(selfptr)
947
- b := mload(otherptr)
948
- }
949
- if (a != b) {
950
- // Mask out irrelevant bytes and check again
951
- uint256 mask = uint256(-1); // 0xffff...
952
- if (shortest < 32) {
953
- mask = ~(2**(8 * (32 - shortest + idx)) - 1);
954
- }
955
- uint256 diff = (a & mask) - (b & mask);
956
- if (diff != 0) return int256(diff);
957
- }
958
- selfptr += 32;
959
- otherptr += 32;
960
- }
961
- return int256(self._len) - int256(other._len);
962
- }
963
-
964
- /*
965
- * @dev Returns true if the two slices contain the same text.
966
- * @param self The first slice to compare.
967
- * @param self The second slice to compare.
968
- * @return True if the slices are equal, false otherwise.
969
- */
970
- function equals(slice memory self, slice memory other)
971
- internal
972
- pure
973
- returns (bool)
974
- {
975
- return compare(self, other) == 0;
976
- }
977
-
978
- /*
979
- * @dev Extracts the first rune in the slice into \`rune\`, advancing the
980
- * slice to point to the next rune and returning \`self\`.
981
- * @param self The slice to operate on.
982
- * @param rune The slice that will contain the first rune.
983
- * @return \`rune\`.
984
- */
985
- function nextRune(slice memory self, slice memory rune)
986
- internal
987
- pure
988
- returns (slice memory)
989
- {
990
- rune._ptr = self._ptr;
991
-
992
- if (self._len == 0) {
993
- rune._len = 0;
994
- return rune;
995
- }
996
-
997
- uint256 l;
998
- uint256 b;
999
- // Load the first byte of the rune into the LSBs of b
1000
- assembly {
1001
- b := and(mload(sub(mload(add(self, 32)), 31)), 0xFF)
1002
- }
1003
- if (b < 0x80) {
1004
- l = 1;
1005
- } else if (b < 0xE0) {
1006
- l = 2;
1007
- } else if (b < 0xF0) {
1008
- l = 3;
1009
- } else {
1010
- l = 4;
1011
- }
1012
-
1013
- // Check for truncated codepoints
1014
- if (l > self._len) {
1015
- rune._len = self._len;
1016
- self._ptr += self._len;
1017
- self._len = 0;
1018
- return rune;
1019
- }
1020
-
1021
- self._ptr += l;
1022
- self._len -= l;
1023
- rune._len = l;
1024
- return rune;
1025
- }
1026
-
1027
- /*
1028
- * @dev Returns the first rune in the slice, advancing the slice to point
1029
- * to the next rune.
1030
- * @param self The slice to operate on.
1031
- * @return A slice containing only the first rune from \`self\`.
1032
- */
1033
- function nextRune(slice memory self)
1034
- internal
1035
- pure
1036
- returns (slice memory ret)
1037
- {
1038
- nextRune(self, ret);
1039
- }
1040
-
1041
- /*
1042
- * @dev Returns the number of the first codepoint in the slice.
1043
- * @param self The slice to operate on.
1044
- * @return The number of the first codepoint in the slice.
1045
- */
1046
- function ord(slice memory self) internal pure returns (uint256 ret) {
1047
- if (self._len == 0) {
1048
- return 0;
1049
- }
1050
-
1051
- uint256 word;
1052
- uint256 length;
1053
- uint256 divisor = 2**248;
1054
-
1055
- // Load the rune into the MSBs of b
1056
- assembly {
1057
- word := mload(mload(add(self, 32)))
1058
- }
1059
- uint256 b = word / divisor;
1060
- if (b < 0x80) {
1061
- ret = b;
1062
- length = 1;
1063
- } else if (b < 0xE0) {
1064
- ret = b & 0x1F;
1065
- length = 2;
1066
- } else if (b < 0xF0) {
1067
- ret = b & 0x0F;
1068
- length = 3;
1069
- } else {
1070
- ret = b & 0x07;
1071
- length = 4;
1072
- }
1073
-
1074
- // Check for truncated codepoints
1075
- if (length > self._len) {
1076
- return 0;
1077
- }
1078
-
1079
- for (uint256 i = 1; i < length; i++) {
1080
- divisor = divisor / 256;
1081
- b = (word / divisor) & 0xFF;
1082
- if (b & 0xC0 != 0x80) {
1083
- // Invalid UTF-8 sequence
1084
- return 0;
1085
- }
1086
- ret = (ret * 64) | (b & 0x3F);
1087
- }
1088
-
1089
- return ret;
1090
- }
1091
-
1092
- /*
1093
- * @dev Returns the keccak-256 hash of the slice.
1094
- * @param self The slice to hash.
1095
- * @return The hash of the slice.
1096
- */
1097
- function keccak(slice memory self) internal pure returns (bytes32 ret) {
1098
- assembly {
1099
- ret := keccak256(mload(add(self, 32)), mload(self))
1100
- }
1101
- }
1102
-
1103
- /*
1104
- * @dev Returns true if \`self\` starts with \`needle\`.
1105
- * @param self The slice to operate on.
1106
- * @param needle The slice to search for.
1107
- * @return True if the slice starts with the provided text, false otherwise.
1108
- */
1109
- function startsWith(slice memory self, slice memory needle)
1110
- internal
1111
- pure
1112
- returns (bool)
1113
- {
1114
- if (self._len < needle._len) {
1115
- return false;
1116
- }
1117
-
1118
- if (self._ptr == needle._ptr) {
1119
- return true;
1120
- }
1121
-
1122
- bool equal;
1123
- assembly {
1124
- let length := mload(needle)
1125
- let selfptr := mload(add(self, 0x20))
1126
- let needleptr := mload(add(needle, 0x20))
1127
- equal := eq(
1128
- keccak256(selfptr, length),
1129
- keccak256(needleptr, length)
1130
- )
1131
- }
1132
- return equal;
1133
- }
1134
-
1135
- /*
1136
- * @dev If \`self\` starts with \`needle\`, \`needle\` is removed from the
1137
- * beginning of \`self\`. Otherwise, \`self\` is unmodified.
1138
- * @param self The slice to operate on.
1139
- * @param needle The slice to search for.
1140
- * @return \`self\`
1141
- */
1142
- function beyond(slice memory self, slice memory needle)
1143
- internal
1144
- pure
1145
- returns (slice memory)
1146
- {
1147
- if (self._len < needle._len) {
1148
- return self;
1149
- }
1150
-
1151
- bool equal = true;
1152
- if (self._ptr != needle._ptr) {
1153
- assembly {
1154
- let length := mload(needle)
1155
- let selfptr := mload(add(self, 0x20))
1156
- let needleptr := mload(add(needle, 0x20))
1157
- equal := eq(
1158
- keccak256(selfptr, length),
1159
- keccak256(needleptr, length)
1160
- )
1161
- }
1162
- }
1163
-
1164
- if (equal) {
1165
- self._len -= needle._len;
1166
- self._ptr += needle._len;
1167
- }
1168
-
1169
- return self;
1170
- }
1171
-
1172
- /*
1173
- * @dev Returns true if the slice ends with \`needle\`.
1174
- * @param self The slice to operate on.
1175
- * @param needle The slice to search for.
1176
- * @return True if the slice starts with the provided text, false otherwise.
1177
- */
1178
- function endsWith(slice memory self, slice memory needle)
1179
- internal
1180
- pure
1181
- returns (bool)
1182
- {
1183
- if (self._len < needle._len) {
1184
- return false;
1185
- }
1186
-
1187
- uint256 selfptr = self._ptr + self._len - needle._len;
1188
-
1189
- if (selfptr == needle._ptr) {
1190
- return true;
1191
- }
1192
-
1193
- bool equal;
1194
- assembly {
1195
- let length := mload(needle)
1196
- let needleptr := mload(add(needle, 0x20))
1197
- equal := eq(
1198
- keccak256(selfptr, length),
1199
- keccak256(needleptr, length)
1200
- )
1201
- }
1202
-
1203
- return equal;
1204
- }
1205
-
1206
- /*
1207
- * @dev If \`self\` ends with \`needle\`, \`needle\` is removed from the
1208
- * end of \`self\`. Otherwise, \`self\` is unmodified.
1209
- * @param self The slice to operate on.
1210
- * @param needle The slice to search for.
1211
- * @return \`self\`
1212
- */
1213
- function until(slice memory self, slice memory needle)
1214
- internal
1215
- pure
1216
- returns (slice memory)
1217
- {
1218
- if (self._len < needle._len) {
1219
- return self;
1220
- }
1221
-
1222
- uint256 selfptr = self._ptr + self._len - needle._len;
1223
- bool equal = true;
1224
- if (selfptr != needle._ptr) {
1225
- assembly {
1226
- let length := mload(needle)
1227
- let needleptr := mload(add(needle, 0x20))
1228
- equal := eq(
1229
- keccak256(selfptr, length),
1230
- keccak256(needleptr, length)
1231
- )
1232
- }
1233
- }
1234
-
1235
- if (equal) {
1236
- self._len -= needle._len;
1237
- }
1238
-
1239
- return self;
1240
- }
1241
-
1242
- // Returns the memory address of the first byte of the first occurrence of
1243
- // \`needle\` in \`self\`, or the first byte after \`self\` if not found.
1244
- function findPtr(
1245
- uint256 selflen,
1246
- uint256 selfptr,
1247
- uint256 needlelen,
1248
- uint256 needleptr
1249
- ) private pure returns (uint256) {
1250
- uint256 ptr = selfptr;
1251
- uint256 idx;
1252
-
1253
- if (needlelen <= selflen) {
1254
- if (needlelen <= 32) {
1255
- bytes32 mask = bytes32(~(2**(8 * (32 - needlelen)) - 1));
1256
-
1257
- bytes32 needledata;
1258
- assembly {
1259
- needledata := and(mload(needleptr), mask)
1260
- }
1261
-
1262
- uint256 end = selfptr + selflen - needlelen;
1263
- bytes32 ptrdata;
1264
- assembly {
1265
- ptrdata := and(mload(ptr), mask)
1266
- }
1267
-
1268
- while (ptrdata != needledata) {
1269
- if (ptr >= end) return selfptr + selflen;
1270
- ptr++;
1271
- assembly {
1272
- ptrdata := and(mload(ptr), mask)
1273
- }
1274
- }
1275
- return ptr;
1276
- } else {
1277
- // For long needles, use hashing
1278
- bytes32 hash;
1279
- assembly {
1280
- hash := keccak256(needleptr, needlelen)
1281
- }
1282
-
1283
- for (idx = 0; idx <= selflen - needlelen; idx++) {
1284
- bytes32 testHash;
1285
- assembly {
1286
- testHash := keccak256(ptr, needlelen)
1287
- }
1288
- if (hash == testHash) return ptr;
1289
- ptr += 1;
1290
- }
1291
- }
1292
- }
1293
- return selfptr + selflen;
1294
- }
1295
-
1296
- // Returns the memory address of the first byte after the last occurrence of
1297
- // \`needle\` in \`self\`, or the address of \`self\` if not found.
1298
- function rfindPtr(
1299
- uint256 selflen,
1300
- uint256 selfptr,
1301
- uint256 needlelen,
1302
- uint256 needleptr
1303
- ) private pure returns (uint256) {
1304
- uint256 ptr;
1305
-
1306
- if (needlelen <= selflen) {
1307
- if (needlelen <= 32) {
1308
- bytes32 mask = bytes32(~(2**(8 * (32 - needlelen)) - 1));
1309
-
1310
- bytes32 needledata;
1311
- assembly {
1312
- needledata := and(mload(needleptr), mask)
1313
- }
1314
-
1315
- ptr = selfptr + selflen - needlelen;
1316
- bytes32 ptrdata;
1317
- assembly {
1318
- ptrdata := and(mload(ptr), mask)
1319
- }
1320
-
1321
- while (ptrdata != needledata) {
1322
- if (ptr <= selfptr) return selfptr;
1323
- ptr--;
1324
- assembly {
1325
- ptrdata := and(mload(ptr), mask)
1326
- }
1327
- }
1328
- return ptr + needlelen;
1329
- } else {
1330
- // For long needles, use hashing
1331
- bytes32 hash;
1332
- assembly {
1333
- hash := keccak256(needleptr, needlelen)
1334
- }
1335
- ptr = selfptr + (selflen - needlelen);
1336
- while (ptr >= selfptr) {
1337
- bytes32 testHash;
1338
- assembly {
1339
- testHash := keccak256(ptr, needlelen)
1340
- }
1341
- if (hash == testHash) return ptr + needlelen;
1342
- ptr -= 1;
1343
- }
1344
- }
1345
- }
1346
- return selfptr;
1347
- }
1348
-
1349
- /*
1350
- * @dev Modifies \`self\` to contain everything from the first occurrence of
1351
- * \`needle\` to the end of the slice. \`self\` is set to the empty slice
1352
- * if \`needle\` is not found.
1353
- * @param self The slice to search and modify.
1354
- * @param needle The text to search for.
1355
- * @return \`self\`.
1356
- */
1357
- function find(slice memory self, slice memory needle)
1358
- internal
1359
- pure
1360
- returns (slice memory)
1361
- {
1362
- uint256 ptr = findPtr(self._len, self._ptr, needle._len, needle._ptr);
1363
- self._len -= ptr - self._ptr;
1364
- self._ptr = ptr;
1365
- return self;
1366
- }
1367
-
1368
- /*
1369
- * @dev Modifies \`self\` to contain the part of the string from the start of
1370
- * \`self\` to the end of the first occurrence of \`needle\`. If \`needle\`
1371
- * is not found, \`self\` is set to the empty slice.
1372
- * @param self The slice to search and modify.
1373
- * @param needle The text to search for.
1374
- * @return \`self\`.
1375
- */
1376
- function rfind(slice memory self, slice memory needle)
1377
- internal
1378
- pure
1379
- returns (slice memory)
1380
- {
1381
- uint256 ptr = rfindPtr(self._len, self._ptr, needle._len, needle._ptr);
1382
- self._len = ptr - self._ptr;
1383
- return self;
1384
- }
1385
-
1386
- /*
1387
- * @dev Splits the slice, setting \`self\` to everything after the first
1388
- * occurrence of \`needle\`, and \`token\` to everything before it. If
1389
- * \`needle\` does not occur in \`self\`, \`self\` is set to the empty slice,
1390
- * and \`token\` is set to the entirety of \`self\`.
1391
- * @param self The slice to split.
1392
- * @param needle The text to search for in \`self\`.
1393
- * @param token An output parameter to which the first token is written.
1394
- * @return \`token\`.
1395
- */
1396
- function split(
1397
- slice memory self,
1398
- slice memory needle,
1399
- slice memory token
1400
- ) internal pure returns (slice memory) {
1401
- uint256 ptr = findPtr(self._len, self._ptr, needle._len, needle._ptr);
1402
- token._ptr = self._ptr;
1403
- token._len = ptr - self._ptr;
1404
- if (ptr == self._ptr + self._len) {
1405
- // Not found
1406
- self._len = 0;
1407
- } else {
1408
- self._len -= token._len + needle._len;
1409
- self._ptr = ptr + needle._len;
1410
- }
1411
- return token;
1412
- }
1413
-
1414
- /*
1415
- * @dev Splits the slice, setting \`self\` to everything after the first
1416
- * occurrence of \`needle\`, and returning everything before it. If
1417
- * \`needle\` does not occur in \`self\`, \`self\` is set to the empty slice,
1418
- * and the entirety of \`self\` is returned.
1419
- * @param self The slice to split.
1420
- * @param needle The text to search for in \`self\`.
1421
- * @return The part of \`self\` up to the first occurrence of \`delim\`.
1422
- */
1423
- function split(slice memory self, slice memory needle)
1424
- internal
1425
- pure
1426
- returns (slice memory token)
1427
- {
1428
- split(self, needle, token);
1429
- }
1430
-
1431
- /*
1432
- * @dev Splits the slice, setting \`self\` to everything before the last
1433
- * occurrence of \`needle\`, and \`token\` to everything after it. If
1434
- * \`needle\` does not occur in \`self\`, \`self\` is set to the empty slice,
1435
- * and \`token\` is set to the entirety of \`self\`.
1436
- * @param self The slice to split.
1437
- * @param needle The text to search for in \`self\`.
1438
- * @param token An output parameter to which the first token is written.
1439
- * @return \`token\`.
1440
- */
1441
- function rsplit(
1442
- slice memory self,
1443
- slice memory needle,
1444
- slice memory token
1445
- ) internal pure returns (slice memory) {
1446
- uint256 ptr = rfindPtr(self._len, self._ptr, needle._len, needle._ptr);
1447
- token._ptr = ptr;
1448
- token._len = self._len - (ptr - self._ptr);
1449
- if (ptr == self._ptr) {
1450
- // Not found
1451
- self._len = 0;
1452
- } else {
1453
- self._len -= token._len + needle._len;
1454
- }
1455
- return token;
1456
- }
1457
-
1458
- /*
1459
- * @dev Splits the slice, setting \`self\` to everything before the last
1460
- * occurrence of \`needle\`, and returning everything after it. If
1461
- * \`needle\` does not occur in \`self\`, \`self\` is set to the empty slice,
1462
- * and the entirety of \`self\` is returned.
1463
- * @param self The slice to split.
1464
- * @param needle The text to search for in \`self\`.
1465
- * @return The part of \`self\` after the last occurrence of \`delim\`.
1466
- */
1467
- function rsplit(slice memory self, slice memory needle)
1468
- internal
1469
- pure
1470
- returns (slice memory token)
1471
- {
1472
- rsplit(self, needle, token);
1473
- }
1474
-
1475
- /*
1476
- * @dev Counts the number of nonoverlapping occurrences of \`needle\` in \`self\`.
1477
- * @param self The slice to search.
1478
- * @param needle The text to search for in \`self\`.
1479
- * @return The number of occurrences of \`needle\` found in \`self\`.
1480
- */
1481
- function count(slice memory self, slice memory needle)
1482
- internal
1483
- pure
1484
- returns (uint256 cnt)
1485
- {
1486
- uint256 ptr = findPtr(self._len, self._ptr, needle._len, needle._ptr) +
1487
- needle._len;
1488
- while (ptr <= self._ptr + self._len) {
1489
- cnt++;
1490
- ptr =
1491
- findPtr(
1492
- self._len - (ptr - self._ptr),
1493
- ptr,
1494
- needle._len,
1495
- needle._ptr
1496
- ) +
1497
- needle._len;
1498
- }
1499
- }
1500
-
1501
- /*
1502
- * @dev Returns True if \`self\` contains \`needle\`.
1503
- * @param self The slice to search.
1504
- * @param needle The text to search for in \`self\`.
1505
- * @return True if \`needle\` is found in \`self\`, false otherwise.
1506
- */
1507
- function contains(slice memory self, slice memory needle)
1508
- internal
1509
- pure
1510
- returns (bool)
1511
- {
1512
- return
1513
- rfindPtr(self._len, self._ptr, needle._len, needle._ptr) !=
1514
- self._ptr;
1515
- }
1516
-
1517
- /*
1518
- * @dev Returns a newly allocated string containing the concatenation of
1519
- * \`self\` and \`other\`.
1520
- * @param self The first slice to concatenate.
1521
- * @param other The second slice to concatenate.
1522
- * @return The concatenation of the two strings.
1523
- */
1524
- function concat(slice memory self, slice memory other)
1525
- internal
1526
- pure
1527
- returns (string memory)
1528
- {
1529
- string memory ret = new string(self._len + other._len);
1530
- uint256 retptr;
1531
- assembly {
1532
- retptr := add(ret, 32)
1533
- }
1534
- memcpy(retptr, self._ptr, self._len);
1535
- memcpy(retptr + self._len, other._ptr, other._len);
1536
- return ret;
1537
- }
1538
-
1539
- /*
1540
- * @dev Joins an array of slices, using \`self\` as a delimiter, returning a
1541
- * newly allocated string.
1542
- * @param self The delimiter to use.
1543
- * @param parts A list of slices to join.
1544
- * @return A newly allocated string containing all the slices in \`parts\`,
1545
- * joined with \`self\`.
1546
- */
1547
- function join(slice memory self, slice[] memory parts)
1548
- internal
1549
- pure
1550
- returns (string memory)
1551
- {
1552
- if (parts.length == 0) return "";
1553
-
1554
- uint256 length = self._len * (parts.length - 1);
1555
- for (uint256 i = 0; i < parts.length; i++) {
1556
- length += parts[i]._len;
1557
- }
1558
-
1559
- string memory ret = new string(length);
1560
- uint256 retptr;
1561
- assembly {
1562
- retptr := add(ret, 32)
1563
- }
1564
-
1565
- for (i = 0; i < parts.length; i++) {
1566
- memcpy(retptr, parts[i]._ptr, parts[i]._len);
1567
- retptr += parts[i]._len;
1568
- if (i < parts.length - 1) {
1569
- memcpy(retptr, self._ptr, self._len);
1570
- retptr += self._len;
1571
- }
1572
- }
1573
-
1574
- return ret;
1575
- }
1576
- }
1577
-
1578
- ================================================================================
1579
- `;