File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -112,3 +112,27 @@ const char[256] HEX_VALUE = {
112112const char [256 ] TO_UPPER @private = { [' a' .. ' z' ] = ' a' - ' A' };
113113const char [256 ] TO_LOWER @private = { [' A' .. ' Z' ] = ' a' - ' A' };
114114
115+ typedef AsciiCharset = uint128 ;
116+
117+ macro AsciiCharset @create_set (String $string ) @const
118+ {
119+ AsciiCharset $set ;
120+ $foreach $c : $string :
121+ $set |= 1ULL << $c ;
122+ $endforeach
123+ return $set ;
124+ }
125+
126+ fn AsciiCharset create_set (String string )
127+ {
128+ AsciiCharset set ;
129+ foreach (c : string ) set |= (AsciiCharset )1ULL << c ;
130+ return set ;
131+ }
132+
133+ macro bool AsciiCharset .contains (set , char c ) => !! (c < 128 ) & !! (set & (AsciiCharset )(1ULL << c ));
134+
135+ const AsciiCharset WHITESPACE_SET = @create_set (" \t\n\v\f\r " );
136+ const AsciiCharset NUMBER_SET = @create_set (" 0123456789" );
137+
138+
Original file line number Diff line number Diff line change 11module std ::core ::string ;
2- import std ::io ;
3- import std ::core ::mem ::allocator ;
2+ import std ::io , std ::ascii ;
43
54
65typedef String @if (! $defined (String )) = inline char [];
@@ -219,6 +218,23 @@ fn String String.trim(self, String to_trim = "\t\n\r ")
219218 return self .trim_left (to_trim ).trim_right (to_trim );
220219}
221220
221+ <*
222+ Remove characters from the front and end of a string.
223+
224+ @param [in] self : `The string to trim`
225+ @param to_trim : `The set of characters to trim, defaults to whitespace`
226+ @pure
227+ @return `a substring of the string passed in`
228+ *>
229+ fn String String .trim_charset (self , AsciiCharset to_trim = ascii ::WHITESPACE_SET )
230+ {
231+ usz start = 0 ;
232+ usz len = self .len ;
233+ while (start < len && to_trim .contains (self [start ])) start ++ ;
234+ while (len > start && to_trim .contains (self [len - 1 ])) len -- ;
235+ return self [start .. len - 1 ];
236+ }
237+
222238<*
223239 Remove characters from the front of a string.
224240
Original file line number Diff line number Diff line change 7272- Added ` HashSet.values ` and ` String.contains_char ` #2386
7373- Added ` &[] ` overload to HashMap.
7474- Deprecated ` PollSubscribes ` and ` PollEvents ` in favour of ` PollSubscribe ` and ` PollEvent ` and made them const enums.
75+ - Added ` AsciiCharset ` for matching ascii characters quickly.
76+ - Added ` String.trim_charset ` .
7577
7678## 0.7.4 Change list
7779
You can’t perform that action at this time.
0 commit comments