-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
pal_normalization.m
91 lines (77 loc) · 2.88 KB
/
pal_normalization.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//
#include "pal_errors.h"
#include "pal_icushim_internal.h"
#include "pal_normalization.h"
#import <Foundation/Foundation.h>
#if !__has_feature(objc_arc)
#error This file relies on ARC for memory management, but ARC is not enabled.
#endif
#if defined(APPLE_HYBRID_GLOBALIZATION)
static NSString* GetNormalizedStringForForm(NormalizationForm normalizationForm, NSString* sourceString)
{
switch (normalizationForm)
{
case FormC:
return sourceString.precomposedStringWithCanonicalMapping;
case FormD:
return sourceString.decomposedStringWithCanonicalMapping;
case FormKC:
return sourceString.precomposedStringWithCompatibilityMapping;
case FormKD:
return sourceString.decomposedStringWithCompatibilityMapping;
default:
return NULL;
}
}
/*
Function:
IsNormalized
Used by System.StringNormalizationExtensions.IsNormalized to detect if a string
is in a certain Unicode Normalization Form.
Return values:
0: lpStr is not normalized.
1: lpStr is normalized.
-1: internal error during normalization.
*/
int32_t GlobalizationNative_IsNormalizedNative(NormalizationForm normalizationForm, const uint16_t* lpStr, int32_t cwStrLength)
{
@autoreleasepool
{
NSString *sourceString = [NSString stringWithCharacters: lpStr length: (NSUInteger)cwStrLength];
NSString *normalizedString = GetNormalizedStringForForm(normalizationForm, sourceString);
return normalizedString == NULL ? -1 : [sourceString isEqualToString: normalizedString];
}
}
/*
Function:
NormalizeString
Used by System.StringNormalizationExtensions.Normalize to normalize a string
into a certain Unicode Normalization Form.
Return values:
0: internal error during normalization.
>0: the length of the normalized string (not counting the null terminator).
*/
int32_t GlobalizationNative_NormalizeStringNative(NormalizationForm normalizationForm, const uint16_t* lpSource, int32_t cwSourceLength, uint16_t* lpDst, int32_t cwDstLength)
{
@autoreleasepool
{
NSString *sourceString = [NSString stringWithCharacters: lpSource length: (NSUInteger)cwSourceLength];
NSString *normalizedString = GetNormalizedStringForForm(normalizationForm, sourceString);
if (normalizedString == NULL || normalizedString.length == 0)
{
return 0;
}
int32_t index = 0, dstIdx = 0, isError = 0;
uint16_t dstCodepoint;
while ((NSUInteger)index < normalizedString.length)
{
dstCodepoint = [normalizedString characterAtIndex: (NSUInteger)index];
Append(lpDst, dstIdx, cwDstLength, dstCodepoint, isError);
index++;
}
return !isError ? (int32_t)[normalizedString length] : 0;
}
}
#endif