forked from react/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndroidUnicodeUtils.java
More file actions
82 lines (75 loc) · 2.82 KB
/
Copy pathAndroidUnicodeUtils.java
File metadata and controls
82 lines (75 loc) · 2.82 KB
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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.hermes.unicode;
import com.facebook.proguard.annotations.DoNotStrip;
import java.text.Collator;
import java.text.DateFormat;
import java.text.Normalizer;
import java.util.Locale;
// TODO: use com.facebook.common.locale.Locales.getApplicationLocale() as the current locale,
// rather than the device locale. This is challenging because getApplicationLocale() is only
// available via DI.
@DoNotStrip
public class AndroidUnicodeUtils {
@DoNotStrip
public static int localeCompare(String left, String right) {
Collator collator = Collator.getInstance();
return collator.compare(left, right);
}
@DoNotStrip
public static String dateFormat(double unixtimeMs, boolean formatDate, boolean formatTime) {
DateFormat format;
if (formatDate && formatTime) {
format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
} else if (formatDate) {
format = DateFormat.getDateInstance(DateFormat.MEDIUM);
} else if (formatTime) {
format = DateFormat.getTimeInstance(DateFormat.MEDIUM);
} else {
throw new RuntimeException("Bad dateFormat configuration");
}
return format.format((long) unixtimeMs).toString();
}
@DoNotStrip
public static String convertToCase(String input, int targetCase, boolean useCurrentLocale) {
// These values must match CaseConversion in PlatformUnicode.h
final int targetUppercase = 0;
final int targetLowercase = 1;
// Note Java's case conversions use the user's locale. For example "I".toLowerCase()
// will produce a dotless i. From Java's docs: "To obtain correct results for locale
// insensitive strings, use toLowerCase(Locale.ENGLISH)."
Locale locale = useCurrentLocale ? Locale.getDefault() : Locale.ENGLISH;
switch (targetCase) {
case targetLowercase:
return input.toLowerCase(locale);
case targetUppercase:
return input.toUpperCase(locale);
default:
throw new RuntimeException("Invalid target case");
}
}
@DoNotStrip
public static String normalize(String input, int form) {
// Values must match NormalizationForm in PlatformUnicode.h.
final int formC = 0;
final int formD = 1;
final int formKC = 2;
final int formKD = 3;
switch (form) {
case formC:
return Normalizer.normalize(input, Normalizer.Form.NFC);
case formD:
return Normalizer.normalize(input, Normalizer.Form.NFD);
case formKC:
return Normalizer.normalize(input, Normalizer.Form.NFKC);
case formKD:
return Normalizer.normalize(input, Normalizer.Form.NFKD);
default:
throw new RuntimeException("Invalid form");
}
}
}