Skip to content

Commit e1f9894

Browse files
committed
stubs: simplify the Unicode stubs
Force the autolinking on Windows and Darwin as both have mechanisms to support this. ELFish targets are unfortunately not supported yet as there is no portable mechanism to do this. Remove the unnecessary handling of specific targets. Always perform the cast as it adds no overhead and will always be correct (worst case is that the type is cast to itself). This simplifies the logic. Move the forward declarations to avoid inclusion to the same location as the inclusion.
1 parent 21d4075 commit e1f9894

File tree

1 file changed

+43
-71
lines changed

1 file changed

+43
-71
lines changed

stdlib/public/stubs/UnicodeNormalization.cpp

Lines changed: 43 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,26 @@
1616

1717
#include "../SwiftShims/UnicodeShims.h"
1818

19-
#if !defined(__APPLE__)
20-
#include "swift/Basic/Lazy.h"
21-
#include "swift/Runtime/Config.h"
22-
#include "swift/Runtime/Debug.h"
19+
#include <stdint.h>
2320

24-
#include <algorithm>
25-
#include <mutex>
26-
#include <assert.h>
21+
#if defined(__APPLE__)
22+
23+
// Declare a few external functions to avoid a dependency on ICU headers.
24+
extern "C" {
25+
typedef struct UBreakIterator UBreakIterator;
26+
typedef enum UBreakIteratorType {} UBreakIteratorType;
27+
typedef enum UErrorCode {} UErrorCode;
28+
typedef uint16_t UChar;
29+
30+
void ubrk_close(UBreakIterator *);
31+
UBreakIterator *ubrk_open(UBreakIteratorType, const char *, const UChar *,
32+
int32_t, UErrorCode *);
33+
int32_t ubrk_preceding(UBreakIterator *, int32_t);
34+
int32_t ubrk_following(UBreakIterator *, int32_t);
35+
void ubrk_setText(UBreakIterator *, const UChar *, int32_t, UErrorCode *);
36+
}
37+
38+
#else
2739

2840
#pragma clang diagnostic push
2941
#pragma clang diagnostic ignored "-Wdocumentation"
@@ -36,6 +48,16 @@
3648

3749
#pragma clang diagnostic pop
3850

51+
#endif
52+
53+
#if !defined(__APPLE__)
54+
#include "swift/Basic/Lazy.h"
55+
#include "swift/Runtime/Config.h"
56+
#include "swift/Runtime/Debug.h"
57+
58+
#include <algorithm>
59+
#include <mutex>
60+
#include <assert.h>
3961

4062
static const UCollator *MakeRootCollator() {
4163
UErrorCode ErrorCode = U_ZERO_ERROR;
@@ -88,11 +110,7 @@ class ASCIICollation {
88110
for (unsigned char c = 0; c < 128; ++c) {
89111
UErrorCode ErrorCode = U_ZERO_ERROR;
90112
intptr_t NumCollationElts = 0;
91-
#if defined(__CYGWIN__) || defined( _MSC_VER) || defined(__linux__)
92113
UChar Buffer[1];
93-
#else
94-
uint16_t Buffer[1];
95-
#endif
96114
Buffer[0] = c;
97115

98116
UCollationElements *CollationIterator =
@@ -129,19 +147,13 @@ swift::_swift_stdlib_unicode_compare_utf16_utf16(const uint16_t *LeftString,
129147
int32_t LeftLength,
130148
const uint16_t *RightString,
131149
int32_t RightLength) {
132-
#if defined(__CYGWIN__) || defined( _MSC_VER) || defined(__linux__)
133150
// ICU UChar type is platform dependent. In Cygwin, it is defined
134151
// as wchar_t which size is 2. It seems that the underlying binary
135152
// representation is same with swift utf16 representation.
136153
// On Clang 4.0 under a recent Linux, ICU uses the built-in char16_t type.
137154
return ucol_strcoll(GetRootCollator(),
138-
reinterpret_cast<const UChar *>(LeftString), LeftLength,
139-
reinterpret_cast<const UChar *>(RightString), RightLength);
140-
#else
141-
return ucol_strcoll(GetRootCollator(),
142-
LeftString, LeftLength,
143-
RightString, RightLength);
144-
#endif
155+
reinterpret_cast<const UChar *>(LeftString), LeftLength,
156+
reinterpret_cast<const UChar *>(RightString), RightLength);
145157
}
146158

147159
/// Compares the strings via the Unicode Collation Algorithm on the root locale.
@@ -159,12 +171,8 @@ swift::_swift_stdlib_unicode_compare_utf8_utf16(const unsigned char *LeftString,
159171
UErrorCode ErrorCode = U_ZERO_ERROR;
160172

161173
uiter_setUTF8(&LeftIterator, reinterpret_cast<const char *>(LeftString), LeftLength);
162-
#if defined(__CYGWIN__) || defined( _MSC_VER) || defined(__linux__)
163174
uiter_setString(&RightIterator, reinterpret_cast<const UChar *>(RightString),
164175
RightLength);
165-
#else
166-
uiter_setString(&RightIterator, RightString, RightLength);
167-
#endif
168176

169177
uint32_t Diff = ucol_strcollIter(GetRootCollator(),
170178
&LeftIterator, &RightIterator, &ErrorCode);
@@ -202,14 +210,9 @@ swift::_swift_stdlib_unicode_compare_utf8_utf8(const unsigned char *LeftString,
202210
void *swift::_swift_stdlib_unicodeCollationIterator_create(
203211
const __swift_uint16_t *Str, __swift_uint32_t Length) {
204212
UErrorCode ErrorCode = U_ZERO_ERROR;
205-
#if defined(__CYGWIN__) || defined( _MSC_VER) || defined(__linux__)
206-
UCollationElements *CollationIterator = ucol_openElements(
207-
GetRootCollator(), reinterpret_cast<const UChar *>(Str), Length,
208-
&ErrorCode);
209-
#else
210-
UCollationElements *CollationIterator = ucol_openElements(
211-
GetRootCollator(), Str, Length, &ErrorCode);
212-
#endif
213+
UCollationElements *CollationIterator =
214+
ucol_openElements(GetRootCollator(), reinterpret_cast<const UChar *>(Str),
215+
Length, &ErrorCode);
213216
if (U_FAILURE(ErrorCode)) {
214217
swift::crash("_swift_stdlib_unicodeCollationIterator_create: ucol_openElements() failed.");
215218
}
@@ -247,17 +250,11 @@ swift::_swift_stdlib_unicode_strToUpper(uint16_t *Destination,
247250
const uint16_t *Source,
248251
int32_t SourceLength) {
249252
UErrorCode ErrorCode = U_ZERO_ERROR;
250-
#if defined(__CYGWIN__) || defined( _MSC_VER) || defined(__linux__)
251253
uint32_t OutputLength = u_strToUpper(reinterpret_cast<UChar *>(Destination),
252254
DestinationCapacity,
253255
reinterpret_cast<const UChar *>(Source),
254256
SourceLength,
255257
"", &ErrorCode);
256-
#else
257-
uint32_t OutputLength = u_strToUpper(Destination, DestinationCapacity,
258-
Source, SourceLength,
259-
"", &ErrorCode);
260-
#endif
261258
if (U_FAILURE(ErrorCode) && ErrorCode != U_BUFFER_OVERFLOW_ERROR) {
262259
swift::crash("u_strToUpper: Unexpected error uppercasing unicode string.");
263260
}
@@ -274,17 +271,11 @@ swift::_swift_stdlib_unicode_strToLower(uint16_t *Destination,
274271
const uint16_t *Source,
275272
int32_t SourceLength) {
276273
UErrorCode ErrorCode = U_ZERO_ERROR;
277-
#if defined(__CYGWIN__) || defined( _MSC_VER) || defined(__linux__)
278274
uint32_t OutputLength = u_strToLower(reinterpret_cast<UChar *>(Destination),
279275
DestinationCapacity,
280276
reinterpret_cast<const UChar *>(Source),
281277
SourceLength,
282278
"", &ErrorCode);
283-
#else
284-
uint32_t OutputLength = u_strToLower(Destination, DestinationCapacity,
285-
Source, SourceLength,
286-
"", &ErrorCode);
287-
#endif
288279
if (U_FAILURE(ErrorCode) && ErrorCode != U_BUFFER_OVERFLOW_ERROR) {
289280
swift::crash("u_strToLower: Unexpected error lowercasing unicode string.");
290281
}
@@ -303,28 +294,6 @@ template <typename T, typename U> const T *ptr_cast(const U *p) {
303294
}
304295
}
305296

306-
#if defined(__APPLE__)
307-
#include <stdint.h>
308-
extern "C" {
309-
// Declare a few external functions to avoid a dependency on ICU headers.
310-
typedef struct UBreakIterator UBreakIterator;
311-
typedef enum UBreakIteratorType {} UBreakIteratorType;
312-
typedef enum UErrorCode {} UErrorCode;
313-
typedef uint16_t UChar;
314-
315-
void ubrk_close(UBreakIterator *);
316-
UBreakIterator *ubrk_open(UBreakIteratorType, const char *, const UChar *,
317-
int32_t, UErrorCode *);
318-
int32_t ubrk_preceding(UBreakIterator *, int32_t);
319-
int32_t ubrk_following(UBreakIterator *, int32_t);
320-
void ubrk_setText(UBreakIterator *, const UChar *, int32_t, UErrorCode *);
321-
}
322-
323-
// Force an autolink with ICU
324-
asm(".linker_option \"-licucore\"\n");
325-
326-
#endif // defined(__APPLE__)
327-
328297
void swift::__swift_stdlib_ubrk_close(
329298
swift::__swift_stdlib_UBreakIterator *bi) {
330299
ubrk_close(ptr_cast<UBreakIterator>(bi));
@@ -333,16 +302,10 @@ void swift::__swift_stdlib_ubrk_close(
333302
swift::__swift_stdlib_UBreakIterator *swift::__swift_stdlib_ubrk_open(
334303
swift::__swift_stdlib_UBreakIteratorType type, const char *locale,
335304
const uint16_t *text, int32_t textLength, __swift_stdlib_UErrorCode *status) {
336-
#if defined(__CYGWIN__) || defined( _MSC_VER) || defined(__linux__)
337305
return ptr_cast<swift::__swift_stdlib_UBreakIterator>(
338306
ubrk_open(static_cast<UBreakIteratorType>(type), locale,
339307
reinterpret_cast<const UChar *>(text), textLength,
340308
ptr_cast<UErrorCode>(status)));
341-
#else
342-
return ptr_cast<swift::__swift_stdlib_UBreakIterator>(
343-
ubrk_open(static_cast<UBreakIteratorType>(type), locale, text, textLength,
344-
ptr_cast<UErrorCode>(status)));
345-
#endif
346309
}
347310

348311
int32_t
@@ -356,9 +319,18 @@ swift::__swift_stdlib_ubrk_following(swift::__swift_stdlib_UBreakIterator *bi,
356319
int32_t offset) {
357320
return ubrk_following(ptr_cast<UBreakIterator>(bi), offset);
358321
}
322+
359323
void swift::__swift_stdlib_ubrk_setText(
360324
swift::__swift_stdlib_UBreakIterator *bi, const __swift_stdlib_UChar *text,
361325
__swift_int32_t textLength, __swift_stdlib_UErrorCode *status) {
362326
return ubrk_setText(ptr_cast<UBreakIterator>(bi), ptr_cast<UChar>(text),
363327
textLength, ptr_cast<UErrorCode>(status));
364328
}
329+
330+
// Force an autolink with ICU
331+
#if defined(__MACH__)
332+
asm(".linker_option \"-licucore\"\n");
333+
#elif defined(_WIN32)
334+
#pragma comment(lib, "icucore.lib")
335+
#endif // defined(__MACH__)
336+

0 commit comments

Comments
 (0)