File tree 1 file changed +28
-17
lines changed
1 file changed +28
-17
lines changed Original file line number Diff line number Diff line change 15
15
use rusqlite:: { named_params, Connection } ;
16
16
use serde:: Deserialize ;
17
17
use sql_support:: ConnExt ;
18
- use std:: hash:: { Hash , Hasher } ;
18
+ use std:: {
19
+ borrow:: Cow ,
20
+ hash:: { Hash , Hasher } ,
21
+ } ;
19
22
use unicase:: UniCase ;
20
23
use unicode_normalization:: { char:: is_combining_mark, UnicodeNormalization } ;
21
24
@@ -225,23 +228,31 @@ pub fn geonames_collate(a: &str, b: &str) -> std::cmp::Ordering {
225
228
UniCase :: new ( collate_remove_chars ( a) ) . cmp ( & UniCase :: new ( collate_remove_chars ( b) ) )
226
229
}
227
230
228
- fn collate_remove_chars ( s : & str ) -> String {
229
- s. nfkd ( )
230
- . filter_map ( |c| {
231
- if is_combining_mark ( c) {
232
- // remove Unicode combining marks ("Que\u{0301}bec" => "Quebec")
233
- None
234
- } else {
235
- match c {
236
- // remove '.' and ',' ("St. Louis, U.S.A." => "St Louis USA")
237
- '.' | ',' => None ,
238
- // replace '-' with space ("Carmel-by-the-Sea" => "Carmel by the Sea")
239
- '-' => Some ( ' ' ) ,
240
- _ => Some ( c) ,
231
+ fn collate_remove_chars ( s : & str ) -> Cow < ' _ , str > {
232
+ let borrowable = !s
233
+ . nfkd ( )
234
+ . any ( |c| is_combining_mark ( c) || matches ! ( c, '.' | ',' | '-' ) ) ;
235
+
236
+ if borrowable {
237
+ Cow :: from ( s)
238
+ } else {
239
+ s. nfkd ( )
240
+ . filter_map ( |c| {
241
+ if is_combining_mark ( c) {
242
+ // remove Unicode combining marks ("Que\u{0301}bec" => "Quebec")
243
+ None
244
+ } else {
245
+ match c {
246
+ // remove '.' and ',' ("St. Louis, U.S.A." => "St Louis USA")
247
+ '.' | ',' => None ,
248
+ // replace '-' with space ("Carmel-by-the-Sea" => "Carmel by the Sea")
249
+ '-' => Some ( ' ' ) ,
250
+ _ => Some ( c) ,
251
+ }
241
252
}
242
- }
243
- } )
244
- . collect :: < String > ( )
253
+ } )
254
+ . collect :: < _ > ( )
255
+ }
245
256
}
246
257
247
258
impl SuggestDao < ' _ > {
You can’t perform that action at this time.
0 commit comments