-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
139 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...-experimental/src/main/java/com/onthegomap/planetiler/experimental/lua/JavaToLuaCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.onthegomap.planetiler.experimental.lua; | ||
|
||
import static java.lang.Character.isDigit; | ||
import static java.lang.Character.isLowerCase; | ||
import static java.lang.Character.isUpperCase; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Converts conventional javaMemberNames to lua_member_names, and lua keywords to uppercase. | ||
*/ | ||
public class JavaToLuaCase { | ||
public static final Set<String> LUA_KEYWORDS = Set.of( | ||
"and", "break", "do", "else", "elseif", | ||
"end", "false", "for", "function", "if", | ||
"in", "local", "nil", "not", "or", | ||
"repeat", "return", "then", "true", "until", "while" | ||
); | ||
private static final String LOWER = "[a-z]"; | ||
private static final String DIGIT = "\\d"; | ||
private static final String UPPER = "[A-Z]"; | ||
private static final String UPPER_OR_DIGIT = "[" + UPPER + DIGIT + "]"; | ||
private static final List<Boundary> BOUNDARIES = List.of( | ||
// getUTF8string -> get_utf8_string | ||
new Boundary(DIGIT, LOWER), | ||
// fooBar -> foo_bar | ||
new Boundary(LOWER, UPPER), | ||
// ASCIIString -> ascii_string, UTF8String -> utf8_string | ||
new Boundary(UPPER_OR_DIGIT, UPPER + LOWER) | ||
); | ||
private static final List<Pattern> BOUNDARY_PATTERNS = BOUNDARIES.stream() | ||
.map(b -> Pattern.compile("(" + b.prev + ")(" + b.next + ")")) | ||
.toList(); | ||
|
||
public static boolean isLowerCamelCase(String fieldName) { | ||
var chars = fieldName.toCharArray(); | ||
if (!isLowerCase(chars[0])) { | ||
return false; | ||
} | ||
boolean upper = false, lower = false, underscore = false; | ||
for (char c : chars) { | ||
upper |= isUpperCase(c) || isDigit(c); | ||
lower |= isLowerCase(c); | ||
underscore |= c == '_'; | ||
} | ||
return upper && lower && !underscore; | ||
} | ||
|
||
public static String transformMemberName(String fieldName) { | ||
if (isLowerCamelCase(fieldName)) { | ||
fieldName = camelToSnake(fieldName); | ||
} | ||
if (LUA_KEYWORDS.contains(fieldName)) { | ||
fieldName = Objects.requireNonNull(fieldName).toUpperCase(Locale.ROOT); | ||
} | ||
return fieldName; | ||
} | ||
|
||
private static String camelToSnake(String fieldName) { | ||
for (Pattern pattern : BOUNDARY_PATTERNS) { | ||
fieldName = pattern.matcher(fieldName).replaceAll("$1_$2"); | ||
} | ||
return fieldName.toLowerCase(); | ||
} | ||
|
||
private record Boundary(String prev, String next) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...erimental/src/test/java/com/onthegomap/planetiler/experimental/lua/JavaToLuaCaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.onthegomap.planetiler.experimental.lua; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
class JavaToLuaCaseTest { | ||
@ParameterizedTest | ||
@CsvSource({ | ||
"foo, foo", | ||
"Foo, Foo", | ||
"fooBar, foo_bar", | ||
"FooBar, FooBar", | ||
"foo_bar, foo_bar", | ||
"foo_BAR, foo_BAR", | ||
"FOO_BAR, FOO_BAR", | ||
"fooBAR, foo_bar", | ||
"getISO3Code, get_iso3_code", | ||
"utf8string, utf8_string", | ||
"arg0, arg0", | ||
"arg10, arg10", | ||
"utf8String, utf8_string", | ||
"getUTF8String, get_utf8_string", | ||
"getUTF8string, get_utf8_string", | ||
"getUTF8ASCIIString, get_utf8ascii_string", | ||
"iso31661Alpha2, iso31661_alpha2", | ||
"getUTF8At0, get_utf8_at0", | ||
"distance3D, distance3d", | ||
"toASCIIString, to_ascii_string", | ||
"and, AND", | ||
}) | ||
void testConversions(String input, String expectedOutput) { | ||
assertEquals(expectedOutput, JavaToLuaCase.transformMemberName(input)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters