Line data Source code
1 : import 'package:batteries/src/iterable.dart' show BatteriesCodeUnitsX;
2 :
3 : /// {@template string.string_extensions}
4 : /// String extensions.
5 : /// {@endtemplate}
6 : extension BatteriesStringX on String {
7 : /// Split string to numbers
8 2 : List<int> splitToNumbers([int max = 8]) => codeUnits
9 1 : .fold<List<int>>(
10 5 : List<int>.generate(max + 1, (i) => i < max ? -1 : 0, growable: false),
11 5 : (r, c) => (c < 48 || c > 57 || r[max] >= max)
12 7 : ? ((r[max] >= max || r[r[max]] < 0) ? r : (r..[max] += 1))
13 10 : : (r..[r[max]] = (r[r[max]] < 0 ? 0 : (r[r[max]] * 10)) + c - 48),
14 : )
15 1 : .take(max)
16 3 : .map<int>((i) => i < 0 ? 0 : i)
17 1 : .toList(growable: false);
18 :
19 : /// Get only latin characters
20 4 : String get onlyLatin => String.fromCharCodes(codeUnits.onlyLatin);
21 :
22 : /// Get only cyrillic characters
23 4 : String get onlyCyrillic => String.fromCharCodes(codeUnits.onlyCyrillic);
24 :
25 : /// Get only digits
26 4 : String get onlyDigit => String.fromCharCodes(codeUnits.onlyDigits);
27 :
28 : /// Transliteration
29 2 : String get transliteration => String.fromCharCodes(
30 5 : codeUnits.expand<int>((e) => _transliterationDictionary[e] ?? <int>[e]),
31 : );
32 : }
33 :
34 : /// Transliteration dictionary
35 : const Map<int, List<int>> _transliterationDictionary = <int, List<int>>{
36 : /// Ё -> Yo
37 : 1025: <int>[89, 111],
38 :
39 : /// А -> A
40 : 1040: <int>[65],
41 :
42 : /// Б -> B
43 : 1041: <int>[66],
44 :
45 : /// В -> V
46 : 1042: <int>[86],
47 :
48 : /// Г -> G
49 : 1043: <int>[71],
50 :
51 : /// Д -> D
52 : 1044: <int>[68],
53 :
54 : /// Е -> E
55 : 1045: <int>[69],
56 :
57 : /// Ж -> Zh
58 : 1046: <int>[90, 104],
59 :
60 : /// З -> Z
61 : 1047: <int>[90],
62 :
63 : /// И -> I
64 : 1048: <int>[73],
65 :
66 : /// Й -> J
67 : 1049: <int>[74],
68 :
69 : /// К -> K
70 : 1050: <int>[75],
71 :
72 : /// Л -> L
73 : 1051: <int>[76],
74 :
75 : /// М -> M
76 : 1052: <int>[77],
77 :
78 : /// Н -> N
79 : 1053: <int>[78],
80 :
81 : /// О -> O
82 : 1054: <int>[79],
83 :
84 : /// П -> P
85 : 1055: <int>[80],
86 :
87 : /// Р -> R
88 : 1056: <int>[82],
89 :
90 : /// С -> S
91 : 1057: <int>[83],
92 :
93 : /// Т -> T
94 : 1058: <int>[84],
95 :
96 : /// У -> U
97 : 1059: <int>[85],
98 :
99 : /// Ф -> F
100 : 1060: <int>[70],
101 :
102 : /// Х -> H
103 : 1061: <int>[72],
104 :
105 : /// Ц -> C
106 : 1062: <int>[67],
107 :
108 : /// Ч -> Ch
109 : 1063: <int>[67, 104],
110 :
111 : /// Ш -> Shh
112 : 1064: <int>[83, 104, 104],
113 :
114 : /// Щ -> Shhch
115 : 1065: <int>[83, 104, 104, 99, 104],
116 :
117 : /// Ы -> Y
118 : 1067: <int>[89],
119 :
120 : /// Э -> Eh'
121 : 1069: <int>[69, 104, 39],
122 :
123 : /// Ю -> Yu
124 : 1070: <int>[89, 117],
125 :
126 : /// Я -> Ya
127 : 1071: <int>[89, 97],
128 :
129 : /// а -> a
130 : 1072: <int>[97],
131 :
132 : /// б -> b
133 : 1073: <int>[98],
134 :
135 : /// в -> v
136 : 1074: <int>[118],
137 :
138 : /// г -> g
139 : 1075: <int>[103],
140 :
141 : /// д -> d
142 : 1076: <int>[100],
143 :
144 : /// е -> e
145 : 1077: <int>[101],
146 :
147 : /// ж -> zh
148 : 1078: <int>[122, 104],
149 :
150 : /// з -> z
151 : 1079: <int>[122],
152 :
153 : /// и -> i
154 : 1080: <int>[105],
155 :
156 : /// й -> j
157 : 1081: <int>[106],
158 :
159 : /// к -> k
160 : 1082: <int>[107],
161 :
162 : /// л -> l
163 : 1083: <int>[108],
164 :
165 : /// м -> m
166 : 1084: <int>[109],
167 :
168 : /// н -> n
169 : 1085: <int>[110],
170 :
171 : /// о -> o
172 : 1086: <int>[111],
173 :
174 : /// п -> p
175 : 1087: <int>[112],
176 :
177 : /// р -> r
178 : 1088: <int>[114],
179 :
180 : /// с -> s
181 : 1089: <int>[115],
182 :
183 : /// т -> t
184 : 1090: <int>[116],
185 :
186 : /// у -> u
187 : 1091: <int>[117],
188 :
189 : /// ф -> f
190 : 1092: <int>[102],
191 :
192 : /// х -> h
193 : 1093: <int>[104],
194 :
195 : /// ц -> c
196 : 1094: <int>[99],
197 :
198 : /// ч -> ch
199 : 1095: <int>[99, 104],
200 :
201 : /// ш -> shh
202 : 1096: <int>[115, 104, 104],
203 :
204 : /// щ -> shhch
205 : 1097: <int>[115, 104, 104, 99, 104],
206 :
207 : /// ъ -> "
208 : 1098: <int>[34],
209 :
210 : /// ы -> y
211 : 1099: <int>[121],
212 :
213 : /// ь -> '
214 : 1100: <int>[39],
215 :
216 : /// э -> eh'
217 : 1101: <int>[101, 104, 39],
218 :
219 : /// ю -> yu
220 : 1102: <int>[121, 117],
221 :
222 : /// я -> ya
223 : 1103: <int>[121, 97],
224 :
225 : /// ё -> yo
226 : 1105: <int>[121, 111],
227 : };
|