| 99 | def limitLength(maxLength as int) as String |
| 100 | return .limitLength(maxLength, nil) |
| 101 | |
| 102 | def limitLength(maxLength as int, suffix as String?) as String |
| 103 | require |
| 104 | suffix implies suffix.length < maxLength |
| 105 | ensure |
| 106 | result.length <= maxLength |
| 107 | test |
| 108 | assert ''.limitLength(2) == '' |
| 109 | assert 'aoeu'.limitLength(2) == 'ao' |
| 110 | assert 'aoeu aoeu aoeu aoeu aoeu'.limitLength(10, '...') == 'aoeu ao...' |
| 111 | |
105 | | def split(chars as List<of char>) as List<of String> |
106 | | test |
107 | | s = 'a,b:c:d,e,f' |
108 | | assert s.split([c',', c':']) == ['a', 'b', 'c', 'd', 'e', 'f'] |
109 | | |
110 | | def split(chars as IList<of char>) as List<of String> |
| 118 | def repeat(times as int) as String |
| 119 | """ |
| 120 | Return the string repeated a number of times. |
| 121 | """ |
| 122 | test |
| 123 | assert 'xy'.repeat(3) == 'xyxyxy' |
| 124 | assert ''.repeat(1_000_000) == '' |
| 125 | |
| 126 | def split(separator as String) as String[] |
| 127 | return .split(separator, int.max, StringSplitOptions.None) |
| 128 | |
| 129 | def split(separator as String, count as int) as String[] |
| 130 | require count >= 0 |
| 131 | return .split(separator, count, StringSplitOptions.None) |
| 132 | |
| 133 | def split(separator as String, options as StringSplitOptions) as String[] |
| 134 | return .split(separator, int.max, options) |
| 135 | |
| 136 | def split(separator as String, count as int, options as StringSplitOptions) as String[] |
| 137 | """ |
| 138 | Return an array of strings created by splitting this string by the given separator |
| 139 | up to a maximum of count items, conforming to the given StringSplitOptions. |
| 140 | """ |
| 141 | require |
| 142 | count >= 0 |
| 143 | test |
| 144 | big = 100 |
| 145 | assert ''.split('aoeu', big) == @[''] |
| 146 | assert 'aoeu'.split(' ', 0) == @[] |
| 147 | assert 'aoeu'.split(' ', big) == @['aoeu'] |
| 148 | assert 'aoeuXasdf'.split('X', big) == @['aoeu', 'asdf'] |
| 149 | assert 'aoeuXXasdf'.split('XX', big) == @['aoeu', 'asdf'] |
| 150 | assert 'aoeuXXasdf'.split('XX', big) == @['aoeu', 'asdf'] |
| 151 | assert 'aoeuXXasdf'.split('XX', 0) == @[] |
| 152 | body |
| 153 | return .split(@[separator], count, options) to ! |
| 154 | |
| 155 | def split(chars as List<of char>) as List<of String> |
| 156 | """ Split this string on any of the list of chars given returning a List of Strings. """ |
| 157 | test |
| 158 | s = 'a,b:c:d,e,f' |
| 159 | assert s.split([c',', c':']) == ['a', 'b', 'c', 'd', 'e', 'f'] |
| 160 | body |
| 161 | return List<of String>(.split(chars.toArray) to !) |
| 162 | |
| 163 | def split(chars as IList<of char>) as List<of String> |