Skip to content

Commit

Permalink
Added Locale.ROOT to toLowerCase() and toUpperCase()
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jan 2, 2025
1 parent 44ae606 commit 15694e1
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/main/java/dev/latvian/mods/rhino/JavaMembers.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -580,7 +581,7 @@ private void reflect(Scriptable scope, boolean includeProtected, Context cx) {
char ch0 = nameComponent.charAt(0);
if (Character.isUpperCase(ch0)) {
if (nameComponent.length() == 1) {
beanPropertyName = nameComponent.toLowerCase();
beanPropertyName = nameComponent.toLowerCase(Locale.ROOT);
} else {
char ch1 = nameComponent.charAt(1);
if (!Character.isUpperCase(ch1)) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/rhino/NativeString.java
Original file line number Diff line number Diff line change
Expand Up @@ -1004,11 +1004,11 @@ public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scrip
}
case Id_toLocaleLowerCase: {
String thisStr = ScriptRuntime.toString(cx, ScriptRuntimeES6.requireObjectCoercible(cx, thisObj, f));
return thisStr.toLowerCase();
return thisStr.toLowerCase(Locale.ROOT);
}
case Id_toLocaleUpperCase: {
String thisStr = ScriptRuntime.toString(cx, ScriptRuntimeES6.requireObjectCoercible(cx, thisObj, f));
return thisStr.toUpperCase();
return thisStr.toUpperCase(Locale.ROOT);
}
case Id_trim: {
String str = ScriptRuntime.toString(cx, ScriptRuntimeES6.requireObjectCoercible(cx, thisObj, f));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dev/latvian/mods/rhino/ScriptableObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;

/**
Expand Down Expand Up @@ -757,7 +758,7 @@ private static String getPropertyName(String methodName, String prefix, Annotati
propName = methodName.substring(3);
if (Character.isUpperCase(propName.charAt(0))) {
if (propName.length() == 1) {
propName = propName.toLowerCase();
propName = propName.toLowerCase(Locale.ROOT);
} else if (!Character.isUpperCase(propName.charAt(1))) {
propName = Character.toLowerCase(propName.charAt(0)) + propName.substring(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
* A list of one or more var, const or let declarations.
Expand Down Expand Up @@ -129,6 +130,6 @@ public void setIsStatement(boolean isStatement) {
}

private String declTypeName() {
return Token.typeToName(type).toLowerCase();
return Token.typeToName(type).toLowerCase(Locale.ROOT);
}
}
33 changes: 28 additions & 5 deletions src/main/java/dev/latvian/mods/rhino/type/EnumTypeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import dev.latvian.mods.rhino.util.RemappedEnumConstant;
import dev.latvian.mods.rhino.util.wrap.TypeWrapperFactory;

import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

Expand All @@ -25,6 +27,7 @@ public static String getName(Object e) {
}

private List<Object> constants;
private Map<String, Object> constantMap;

EnumTypeInfo(Class<?> type) {
super(type);
Expand All @@ -48,21 +51,41 @@ public Object wrap(Context cx, Object from, TypeInfo target) {
return null;
}

for (var entry : enumConstants()) {
var constants = enumConstants();

if (constantMap == null) {
constantMap = new HashMap<>(constants.size());

for (var entry : constants) {
var name = getName(entry);
constantMap.put(name.toLowerCase(Locale.ROOT), entry);
constantMap.put(name, entry);
}
}

var lookup = constantMap.get(s);

if (lookup != null) {
return lookup;
}

for (var entry : constants) {
if (getName(entry).equalsIgnoreCase(s)) {
return entry;
}
}

throw new IllegalArgumentException("'" + s + "' is not a valid enum constant! Valid values are: " + enumConstants().stream().map(EnumTypeInfo::getName).map(s1 -> "'" + s1 + "'").collect(Collectors.joining(", ")));
throw new IllegalArgumentException("'" + s + "' is not a valid enum constant! Valid values are: " + constants.stream().map(EnumTypeInfo::getName).map(s1 -> "'" + s1 + "'").collect(Collectors.joining(", ")));
} else if (from instanceof Number) {
int index = ((Number) from).intValue();

if (index < 0 || index >= enumConstants().size()) {
throw new IllegalArgumentException(index + " is not a valid enum index! Valid values are: 0 - " + (enumConstants().size() - 1));
var constants = enumConstants();

if (index < 0 || index >= constants.size()) {
throw new IllegalArgumentException(index + " is not a valid enum index! Valid values are: 0 - " + (constants.size() - 1));
}

return enumConstants().get(index);
return constants.get(index);
}

return from;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.latvian.mods.rhino.util;

import java.util.Locale;

public enum DefaultValueTypeHint {
STRING,
NUMBER,
Expand All @@ -10,7 +12,7 @@ public enum DefaultValueTypeHint {
public final String name;

DefaultValueTypeHint() {
name = name().toLowerCase();
name = name().toLowerCase(Locale.ROOT);
}

public String toString() {
Expand Down

0 comments on commit 15694e1

Please sign in to comment.