Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,17 @@ else if ( name.isQuoted() ) {

private String camelCaseToSnakeCase(String name) {
final StringBuilder builder = new StringBuilder( name.replace( '.', '_' ) );
for ( int i = 1; i < builder.length() - 1; i++ ) {
if ( isUnderscoreRequired( builder.charAt( i - 1 ), builder.charAt( i ), builder.charAt( i + 1 ) ) ) {
builder.insert( i++, '_' );
final char[] copy = builder.toString().toCharArray();
int builderIndex = 1;
for ( int i = 1; i <= copy.length - 1; i++ ) {
if ( isUnderscoreRequired(
copy[i - 1],
copy[i],
i == copy.length - 1 ? '*' : copy[i + 1] ) // add some fictitious final character
) {
builder.insert( builderIndex++, '_' );
}
builderIndex++;
}
Comment on lines +63 to 74
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that this would be simpler and avoid creating the char array copy.

Suggested change
final char[] copy = builder.toString().toCharArray();
int builderIndex = 1;
for ( int i = 1; i <= copy.length - 1; i++ ) {
if ( isUnderscoreRequired(
copy[i - 1],
copy[i],
i == copy.length - 1 ? '*' : copy[i + 1] ) // add some fictitious final character
) {
builder.insert( builderIndex++, '_' );
}
builderIndex++;
}
for ( int i = 1; i < builder.length() - 1; i++ ) {
if ( isUnderscoreRequired( builder.charAt( i - 1 ), builder.charAt( i ), builder.charAt( i + 1 ) ) ) {
builder.insert( i++, '_' );
}
}
if ( builder.length() > 1 && isUnderscoreRequired( copy[builder.length() - 2], copy[builder.length() - 1], '0' ) ) {
builder.insert( builder.length() - 1, '_' );
}

Copy link
Member Author

@jrenaat jrenaat Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the char[] copy to avoid the index increment inside the for loop, which is what caused the jump over the last character (and it makes it somewhat hard to follow, and it's imo ugly (arguable))
Also, your change still uses the copy ?

return builder.toString();
}
Expand All @@ -79,6 +86,6 @@ protected Identifier quotedIdentifier(Identifier quotedName) {
private boolean isUnderscoreRequired(final char before, final char current, final char after) {
return ( isLowerCase( before ) || isDigit( before ) )
&& isUpperCase( current )
&& ( isLowerCase( after ) || isDigit( after ) );
&& ( isLowerCase( after ) || isDigit( after ) || after == '*' );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,22 @@ public void testWithWordWithDigitNamingStrategy() {
.isEqualTo( "hello3d4" );
assertThat( entityBinding.getProperty( "quoted" ).getSelectables().get( 0 ).getText() )
.isEqualTo( "Quoted-ColumnName" );
assertThat( entityBinding.getProperty( "wordX" ).getSelectables().get( 0 ).getText() )
.isEqualTo( "word_x" );
}

@Entity
@Table(name = "ABCD")
class B implements java.io.Serializable {
private static class B implements java.io.Serializable {
@Id
protected String AbcdEfghI21;
protected String wordWithDigitD1;
protected String hello1;
protected String hello1D2;
protected String hello3d4;
private String AbcdEfghI21;
private String wordWithDigitD1;
private String hello1;
private String hello1D2;
private String hello3d4;
@Column(name = "\"Quoted-ColumnName\"")
protected String quoted;
private String quoted;
private String wordX;

public String getAbcdEfghI21() {
return AbcdEfghI21;
Expand Down
Loading