44#if os(Windows)
55import WindowsCore
66#else
7+ import POSIXCore
8+ import Synchronization
9+ #if canImport(Darwin)
10+ import Darwin
11+ import xlocale_wrapper
12+ #elseif canImport(Glibc)
13+ import Glibc
14+ #endif
715#if GNU
816import libunistring
917#endif
1018import POSIXCore
1119import Synchronization
20+ #endif
1221
22+ #if canImport(Glibc)
1323private enum Locale {
1424 private static let utf8 : Mutex < locale_t ? > = Mutex ( nil )
1525
@@ -21,7 +31,18 @@ private enum Locale {
2131 }
2232 }
2333}
34+ #elseif canImport(Darwin)
35+ private enum Locale {
36+ private static let utf8 : Mutex < UnsafeMutableRawPointer ? > = Mutex ( nil )
2437
38+ static var ID_UTF8 : UnsafeMutableRawPointer ? {
39+ return utf8. withLock { locale in
40+ if let locale { return locale }
41+ locale = vt_newlocale ( LC_CTYPE_MASK, " en_US.UTF-8 " , nil )
42+ return locale
43+ }
44+ }
45+ }
2546#endif
2647
2748extension UnicodeScalar {
@@ -87,12 +108,14 @@ extension UnicodeScalar {
87108 // Normal width character -> 1
88109 // Wide character (CJK, etc.) -> 2
89110 return max ( 1 , Int ( uc_width ( UInt32 ( value) , " C.UTF-8 " ) ) )
90- #else
111+ #elseif canImport(Glibc)
91112 // Control character or invalid - zero width -> -1
92113 // Zero-width character (combining marks, etc.) -> 0
93114 // Normal width character -> 1
94115 // Wide character (CJK, etc.) -> 2
95116 return max ( 1 , Int ( wcwidth_l ( wchar_t ( value) , Locale . ID_UTF8) ) )
117+ #else
118+ return max ( 1 , Int ( vt_wcwidth_l ( wchar_t ( value) , Locale . ID_UTF8) ) )
96119#endif
97120 }
98121}
0 commit comments