Skip to content

Commit

Permalink
String apis
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberpwnn committed Sep 5, 2021
1 parent 06213a0 commit e376c83
Showing 1 changed file with 255 additions and 0 deletions.
255 changes: 255 additions & 0 deletions src/main/java/extensions/java/lang/String/XString.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import manifold.ext.rt.api.This;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Extension
public class XString {
Expand Down Expand Up @@ -47,4 +49,257 @@ public static String normalize(@This String s)
return buf.trim();
}

/**
* Capitalize the first letter
*
* @param s the string
* @return the capitalized string
*/
public static String capitalize(@This String s) {
StringBuilder roll = new StringBuilder();
boolean f = true;

for (Character i : s.trim().toCharArray()) {
if (f) {
roll.append(Character.toUpperCase(i));
f = false;
} else {
roll.append(i);
}
}

return roll.toString();
}

/**
* Capitalize all words in the string
*
* @param s the string
* @return the capitalized string
*/
public static String capitalizeWords(@This String s) {
StringBuilder rollx = new StringBuilder();

for (String i : s.trim().split(" ")) {
rollx.append(" ").append(capitalize(i.trim()));
}

return rollx.substring(1);
}

/**
* Hard word wrap
*
* @param s the words
* @param len the length per line
* @return the wrapped string
*/
public static String wrap(@This String s, int len) {
return wrap(s, len, null, false);
}

/**
* Soft Word wrap
*
* @param s the string
* @param len the length to wrap
* @return the wrapped string
*/
public static String wrapWords(@This String s, int len) {
return wrap(s, len, null, true);
}

/**
* Wrap words
*
* @param s the string
* @param len the wrap length
* @param newLineSep the new line seperator
* @param soft should it be soft wrapped or hard wrapped?
* @return the wrapped words
*/
public static String wrap(@This String s, int len, String newLineSep, boolean soft) {
return wrap(s, len, newLineSep, soft, " ");
}

/**
* Wrap words
*
* @param s the string
* @param len the length
* @param newLineSep the new line seperator
* @param soft soft or hard wrapping
* @param regex the regex
* @return the wrapped string
*/
public static String wrap(@This String s, int len, String newLineSep, boolean soft, String regex) {
if (s == null) {
return null;
} else {
if (newLineSep == null) {
newLineSep = "\n";
}

if (len < 1) {
len = 1;
}

if (regex.trim().equals("")) {
regex = " ";
}

Pattern arg4 = Pattern.compile(regex);
int arg5 = s.length();
int arg6 = 0;
StringBuilder arg7 = new StringBuilder(arg5 + 32);

while (arg6 < arg5) {
int arg8 = -1;
Matcher arg9 = arg4.matcher(s.substring(arg6, Math.min(arg6 + len + 1, arg5)));
if (arg9.find()) {
if (arg9.start() == 0) {
arg6 += arg9.end();
continue;
}

arg8 = arg9.start();
}

if (arg5 - arg6 <= len) {
break;
}

while (arg9.find()) {
arg8 = arg9.start() + arg6;
}

if (arg8 >= arg6) {
arg7.append(s, arg6, arg8);
arg7.append(newLineSep);
arg6 = arg8 + 1;
} else if (soft) {
arg7.append(s, arg6, len + arg6);
arg7.append(newLineSep);
arg6 += len;
} else {
arg9 = arg4.matcher(s.substring(arg6 + len));
if (arg9.find()) {
arg8 = arg9.start() + arg6 + len;
}

if (arg8 >= 0) {
arg7.append(s, arg6, arg8);
arg7.append(newLineSep);
arg6 = arg8 + 1;
} else {
arg7.append(s.substring(arg6));
arg6 = arg5;
}
}
}

arg7.append(s.substring(arg6));
return arg7.toString();
}
}

/**
* Trim a string to a length, then append ... at the end if it extends the limit
*
* @param s the string
* @param l the limit
* @return the modified string
*/
public static String elipse(@This String s, int l) {
if (s.length() <= l) {
return s;
}

return s.substring(0, l) + "...";
}

/**
* Get the number representation from roman numerals.
*
* @param number the roman number
* @return the int representation
*/
public static int fromRoman(@This String number) {
if (number.isEmpty()) {
return 0;
}

number = number.toUpperCase();

if (number.startsWith("M")) {
return 1000 + fromRoman(number.substring(1));
}

if (number.startsWith("CM")) {
return 900 + fromRoman(number.substring(2));
}

if (number.startsWith("D")) {
return 500 + fromRoman(number.substring(1));
}

if (number.startsWith("CD")) {
return 400 + fromRoman(number.substring(2));
}

if (number.startsWith("C")) {
return 100 + fromRoman(number.substring(1));
}

if (number.startsWith("XC")) {
return 90 + fromRoman(number.substring(2));
}

if (number.startsWith("L")) {
return 50 + fromRoman(number.substring(1));
}

if (number.startsWith("XL")) {
return 40 + fromRoman(number.substring(2));
}

if (number.startsWith("X")) {
return 10 + fromRoman(number.substring(1));
}

if (number.startsWith("IX")) {
return 9 + fromRoman(number.substring(2));
}

if (number.startsWith("V")) {
return 5 + fromRoman(number.substring(1));
}

if (number.startsWith("IV")) {
return 4 + fromRoman(number.substring(2));
}

if (number.startsWith("I")) {
return 1 + fromRoman(number.substring(1));
}

return 0;
}

/**
* Scroll text
*
* @param smx the text
* @param viewport the viewport length
* @param time the timeline value
*/
public static String scroll(@This String smx, int viewport, long time) {
String src = " ".repeat(viewport) + smx + " ".repeat(viewport);
int len = src.length();
int walk = (int) (time % (len - viewport));
String base = src.substring(walk, Math.min(walk + viewport, len - 1));
base = base.length() < viewport ? base + " ".repeat((viewport - base.length()) - 3) : base;

return base;
}
}

0 comments on commit e376c83

Please sign in to comment.