Skip to content

Commit 531c87d

Browse files
committed
Restore deleted AndroidUnicodeUtils.java
1 parent 3b3584d commit 531c87d

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.hermes.unicode;
9+
10+
import com.facebook.proguard.annotations.DoNotStrip;
11+
import java.text.Collator;
12+
import java.text.DateFormat;
13+
import java.text.Normalizer;
14+
import java.util.Locale;
15+
16+
// TODO: use com.facebook.common.locale.Locales.getApplicationLocale() as the current locale,
17+
// rather than the device locale. This is challenging because getApplicationLocale() is only
18+
// available via DI.
19+
@DoNotStrip
20+
public class AndroidUnicodeUtils {
21+
@DoNotStrip
22+
public static int localeCompare(String left, String right) {
23+
Collator collator = Collator.getInstance();
24+
return collator.compare(left, right);
25+
}
26+
27+
@DoNotStrip
28+
public static String dateFormat(double unixtimeMs, boolean formatDate, boolean formatTime) {
29+
DateFormat format;
30+
if (formatDate && formatTime) {
31+
format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
32+
} else if (formatDate) {
33+
format = DateFormat.getDateInstance(DateFormat.MEDIUM);
34+
} else if (formatTime) {
35+
format = DateFormat.getTimeInstance(DateFormat.MEDIUM);
36+
} else {
37+
throw new RuntimeException("Bad dateFormat configuration");
38+
}
39+
return format.format((long) unixtimeMs).toString();
40+
}
41+
42+
@DoNotStrip
43+
public static String convertToCase(String input, int targetCase, boolean useCurrentLocale) {
44+
// These values must match CaseConversion in PlatformUnicode.h
45+
final int targetUppercase = 0;
46+
final int targetLowercase = 1;
47+
// Note Java's case conversions use the user's locale. For example "I".toLowerCase()
48+
// will produce a dotless i. From Java's docs: "To obtain correct results for locale
49+
// insensitive strings, use toLowerCase(Locale.ENGLISH)."
50+
Locale locale = useCurrentLocale ? Locale.getDefault() : Locale.ENGLISH;
51+
switch (targetCase) {
52+
case targetLowercase:
53+
return input.toLowerCase(locale);
54+
case targetUppercase:
55+
return input.toUpperCase(locale);
56+
default:
57+
throw new RuntimeException("Invalid target case");
58+
}
59+
}
60+
61+
@DoNotStrip
62+
public static String normalize(String input, int form) {
63+
// Values must match NormalizationForm in PlatformUnicode.h.
64+
final int formC = 0;
65+
final int formD = 1;
66+
final int formKC = 2;
67+
final int formKD = 3;
68+
69+
switch (form) {
70+
case formC:
71+
return Normalizer.normalize(input, Normalizer.Form.NFC);
72+
case formD:
73+
return Normalizer.normalize(input, Normalizer.Form.NFD);
74+
case formKC:
75+
return Normalizer.normalize(input, Normalizer.Form.NFKC);
76+
case formKD:
77+
return Normalizer.normalize(input, Normalizer.Form.NFKD);
78+
default:
79+
throw new RuntimeException("Invalid form");
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)