-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hyphenate @RestHeader name by default
- Loading branch information
Showing
4 changed files
with
92 additions
and
3 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
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
85 changes: 85 additions & 0 deletions
85
...mmon/processor/src/main/java/org/jboss/resteasy/reactive/common/processor/StringUtil.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,85 @@ | ||
package org.jboss.resteasy.reactive.common.processor; | ||
|
||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
public class StringUtil { | ||
|
||
public static Iterator<String> camelHumpsIterator(final String str) { | ||
return new Iterator<String>() { | ||
int idx; | ||
|
||
public boolean hasNext() { | ||
return idx < str.length(); | ||
} | ||
|
||
public String next() { | ||
if (idx == str.length()) | ||
throw new NoSuchElementException(); | ||
// known mixed-case rule-breakers | ||
if (str.startsWith("JBoss", idx)) { | ||
idx += 5; | ||
return "JBoss"; | ||
} | ||
final int start = idx; | ||
int c = str.codePointAt(idx); | ||
if (Character.isUpperCase(c)) { | ||
// an uppercase-starting word | ||
idx = str.offsetByCodePoints(idx, 1); | ||
if (idx < str.length()) { | ||
c = str.codePointAt(idx); | ||
if (Character.isUpperCase(c)) { | ||
// all-caps word; need one look-ahead | ||
int nextIdx = str.offsetByCodePoints(idx, 1); | ||
while (nextIdx < str.length()) { | ||
c = str.codePointAt(nextIdx); | ||
if (Character.isLowerCase(c)) { | ||
// ended at idx | ||
return str.substring(start, idx); | ||
} | ||
idx = nextIdx; | ||
nextIdx = str.offsetByCodePoints(idx, 1); | ||
} | ||
// consumed the whole remainder, update idx to length | ||
idx = str.length(); | ||
return str.substring(start); | ||
} else { | ||
// initial caps, trailing lowercase | ||
idx = str.offsetByCodePoints(idx, 1); | ||
while (idx < str.length()) { | ||
c = str.codePointAt(idx); | ||
if (Character.isUpperCase(c)) { | ||
// end | ||
return str.substring(start, idx); | ||
} | ||
idx = str.offsetByCodePoints(idx, 1); | ||
} | ||
// consumed the whole remainder | ||
return str.substring(start); | ||
} | ||
} else { | ||
// one-letter word | ||
return str.substring(start); | ||
} | ||
} else { | ||
// a lowercase-starting word | ||
idx = str.offsetByCodePoints(idx, 1); | ||
while (idx < str.length()) { | ||
c = str.codePointAt(idx); | ||
if (Character.isUpperCase(c)) { | ||
// end | ||
return str.substring(start, idx); | ||
} | ||
idx = str.offsetByCodePoints(idx, 1); | ||
} | ||
// consumed the whole remainder | ||
return str.substring(start); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public static String hyphenate(final String orig) { | ||
return String.join("-", (Iterable<String>) () -> camelHumpsIterator(orig)); | ||
} | ||
} |