org.joinedworkz.common.helper.StringHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-base Show documentation
Show all versions of common-base Show documentation
DSL based modeling framework - facilities common base
package org.joinedworkz.common.helper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.inject.Singleton;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
@Singleton
@SuppressWarnings("all")
public class StringHelper {
public static final char NL = '\n';
public static final char CR = '\r';
public static final char BRACKET_OPEN = '(';
public static final char BRACKET_CLOSE = ')';
public boolean isNewLineChar(final char c) {
return ((c == StringHelper.NL) || (c == StringHelper.CR));
}
public boolean beginsLineChar(final StringBuilder buffer) {
return ((buffer.length() == 0) || this.isNewLineChar(buffer.charAt((buffer.length() - 1))));
}
public String trimLastLineBreak(final String str) {
if ((str != null)) {
int totalCount = 0;
int crCount = 0;
int nlCount = 0;
for (int i = (str.length() - 1); (i >= 0); i--) {
{
totalCount++;
final char c = str.charAt(i);
if ((c == StringHelper.CR)) {
crCount++;
}
if ((c == StringHelper.NL)) {
nlCount++;
}
if ((totalCount > (crCount + nlCount))) {
if ((totalCount == 1)) {
return str;
} else {
int _length = str.length();
int _minus = (_length - totalCount);
int _plus = (_minus + 1);
return str.substring(0, _plus);
}
}
}
}
}
return null;
}
public String textInLineAfterMarker(final String string, final String markerString) {
if ((string != null)) {
final int markerIndex = string.indexOf(markerString);
if ((markerIndex >= 0)) {
int _length = markerString.length();
int _plus = (_length + 1);
return this.contentOfLineAfter(string, _plus);
}
}
return null;
}
public String contentOfLineBefore(final String str, final int index) {
for (int i = index; (i >= 0); i--) {
{
final char c = str.charAt(i);
if (((c == StringHelper.CR) || (c == StringHelper.NL))) {
return str.substring((i + 1), index);
}
}
}
return str;
}
public String contentOfLineAfter(final String str, final int index) {
final int strLength = str.length();
for (int i = index; (i < strLength); i++) {
{
final char c = str.charAt(i);
if (((c == StringHelper.CR) || (c == StringHelper.NL))) {
return str.substring(index, i);
}
}
}
return str.substring(index);
}
public int indexOfLineBreakAfter(final String str, final int index) {
final int strLength = str.length();
for (int i = index; (i < strLength); i++) {
{
final char c = str.charAt(i);
if (((c == StringHelper.CR) || (c == StringHelper.NL))) {
return i;
}
}
}
return (-1);
}
public boolean containsLineBreak(final String str) {
if ((str != null)) {
return ((str.indexOf(StringHelper.CR) >= 0) || (str.indexOf(StringHelper.NL) >= 0));
} else {
return false;
}
}
public void insertLinesOfString(final StringBuilder buffer, final String str, final String indention) {
this.insertLinesOfString(buffer, str, indention, true);
}
public void insertLinesOfString(final StringBuilder buffer, final String str, final String indention, final boolean ignoreBlankString) {
if (ignoreBlankString) {
int _length = str.trim().length();
final boolean hasContent = (_length > 0);
if ((!hasContent)) {
return;
}
}
final String[] lines = str.split("\\r?\\n");
int _length_1 = str.length();
int _minus = (_length_1 - 1);
final char lastChar = str.charAt(_minus);
int _length_2 = indention.length();
final boolean hasIndention = (_length_2 > 0);
final int lineCount = lines.length;
final int lastLine = (lineCount - 1);
for (int i = 0; (i < lineCount); i++) {
{
final String line = lines[i];
if ((hasIndention && this.beginsLineChar(buffer))) {
buffer.append(indention);
}
buffer.append(line);
if (((i < lastLine) || this.isNewLineChar(lastChar))) {
this.addLineBreak(buffer);
}
}
}
if ((((!ignoreBlankString) && (lineCount == 0)) && (str.contains("\r") || str.contains("\n")))) {
buffer.append(str);
}
}
public StringBuilder addLineBreak(final StringBuilder buffer) {
return buffer.append("\r\n");
}
public int indexOfKeywordOnSameLevel(final int startIndex, final String sql, final String keyword) {
int _length = sql.length();
int _minus = (_length - 1);
boolean _equals = (startIndex == _minus);
if (_equals) {
return (-1);
}
final String[] parts = sql.substring(startIndex).split(keyword);
int _length_1 = parts.length;
boolean _equals_1 = (_length_1 == 1);
if (_equals_1) {
return (-1);
}
int index = startIndex;
int subLevel = 0;
for (final String part : parts) {
{
final int partLength = part.length();
for (int i = 0; (i < partLength); i++) {
{
final char c = part.charAt(i);
boolean _equals_2 = Objects.equals(Character.valueOf(c), "(");
if (_equals_2) {
subLevel++;
} else {
boolean _equals_3 = Objects.equals(Character.valueOf(c), ")");
if (_equals_3) {
subLevel--;
}
}
}
}
int _index = index;
index = (_index + partLength);
if ((subLevel == 0)) {
return index;
} else {
int _index_1 = index;
index = (_index_1 + 4);
}
}
}
return (-1);
}
public List splitByCharacterOnSameLevel(final String sql, final char splitBy) {
if ((sql == null)) {
return Collections.emptyList();
}
int _indexOf = sql.indexOf(splitBy);
boolean _tripleEquals = (_indexOf == (-1));
if (_tripleEquals) {
return List.of(sql);
}
final ArrayList parts = CollectionLiterals.newArrayList();
int begin = 0;
int subLevel = 0;
int index = 0;
final int length = sql.length();
for (; (index < length); index++) {
{
final char c = sql.charAt(index);
if ((c == StringHelper.BRACKET_OPEN)) {
subLevel++;
} else {
if ((c == StringHelper.BRACKET_CLOSE)) {
subLevel--;
}
}
if (((subLevel == 0) && (c == splitBy))) {
final String part = sql.substring(begin, index);
parts.add(part);
begin = (index + 1);
}
}
}
final String lastPart = sql.substring(begin, index);
parts.add(lastPart);
return parts;
}
}