2021-01-10 23:37:09 +01:00
|
|
|
/* Copyright (C) 2017-2021 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
|
|
|
Gobbetti, João Paulo Barraca, Nephiel, Roi Greenberg, Taavi Eomäe,
|
|
|
|
Zhong Jianxin
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2017-01-24 02:44:30 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
|
|
|
|
2020-06-15 11:44:58 +02:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.CharBuffer;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
2019-01-26 15:52:40 +01:00
|
|
|
import androidx.annotation.NonNull;
|
2022-08-18 23:03:28 +02:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
2017-03-02 00:27:54 +01:00
|
|
|
|
2017-01-24 02:44:30 +01:00
|
|
|
public class StringUtils {
|
|
|
|
|
2018-08-21 20:27:00 +02:00
|
|
|
|
|
|
|
|
2017-03-18 11:41:47 +01:00
|
|
|
@NonNull
|
2017-01-24 02:44:30 +01:00
|
|
|
public static String truncate(String s, int maxLength){
|
2017-03-02 00:27:54 +01:00
|
|
|
if (s == null) {
|
|
|
|
return "";
|
|
|
|
}
|
2017-01-24 02:44:30 +01:00
|
|
|
|
2017-03-02 00:27:54 +01:00
|
|
|
int length = Math.min(s.length(), maxLength);
|
2018-08-21 20:51:10 +02:00
|
|
|
if(length < 0) {
|
2017-01-24 02:44:30 +01:00
|
|
|
return "";
|
2017-03-02 00:27:54 +01:00
|
|
|
}
|
2017-01-24 02:44:30 +01:00
|
|
|
|
|
|
|
return s.substring(0, length);
|
|
|
|
}
|
|
|
|
|
2020-06-15 11:44:58 +02:00
|
|
|
public static int utf8ByteLength(String string, int length) {
|
|
|
|
if (string == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ByteBuffer outBuf = ByteBuffer.allocate(length);
|
|
|
|
CharBuffer inBuf = CharBuffer.wrap(string.toCharArray());
|
|
|
|
StandardCharsets.UTF_8.newEncoder().encode(inBuf, outBuf, true);
|
|
|
|
return outBuf.position();
|
|
|
|
}
|
|
|
|
|
2017-01-24 02:44:30 +01:00
|
|
|
public static String pad(String s, int length){
|
|
|
|
return pad(s, length, ' ');
|
|
|
|
}
|
|
|
|
|
2020-08-26 04:28:03 +02:00
|
|
|
public static String pad(String s, int length, char padChar) {
|
|
|
|
StringBuilder sBuilder = new StringBuilder(s);
|
|
|
|
while (sBuilder.length() < length) {
|
|
|
|
sBuilder.append(padChar);
|
2017-03-02 00:27:54 +01:00
|
|
|
}
|
2020-08-26 04:28:03 +02:00
|
|
|
s = sBuilder.toString();
|
2017-01-24 02:44:30 +01:00
|
|
|
return s;
|
|
|
|
}
|
2017-03-02 00:27:54 +01:00
|
|
|
|
2017-03-11 22:15:44 +01:00
|
|
|
/**
|
|
|
|
* Joins the given elements and adds a separator between each element in the resulting string.
|
|
|
|
* There will be no separator at the start or end of the string. There will be no consecutive
|
|
|
|
* separators (even in case an element is null or empty).
|
|
|
|
* @param separator the separator string
|
|
|
|
* @param elements the elements to concatenate to a new string
|
|
|
|
* @return the joined strings, separated by the separator
|
|
|
|
*/
|
2017-03-02 00:27:54 +01:00
|
|
|
@NonNull
|
2017-03-11 22:15:44 +01:00
|
|
|
public static StringBuilder join(String separator, String... elements) {
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
if (elements == null) {
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
boolean hasAdded = false;
|
|
|
|
for (String element : elements) {
|
|
|
|
if (element != null && element.length() > 0) {
|
|
|
|
if (hasAdded) {
|
|
|
|
builder.append(separator);
|
|
|
|
}
|
|
|
|
builder.append(element);
|
|
|
|
hasAdded = true;
|
|
|
|
}
|
2017-03-02 00:27:54 +01:00
|
|
|
}
|
2017-03-11 22:15:44 +01:00
|
|
|
return builder;
|
2017-03-02 00:27:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
public static String getFirstOf(String first, String second) {
|
|
|
|
if (first != null && first.length() > 0) {
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
if (second != null) {
|
|
|
|
return second;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2017-03-14 00:44:59 +01:00
|
|
|
|
2022-06-23 23:12:08 +02:00
|
|
|
public static boolean isNullOrEmpty(String string){
|
|
|
|
return string == null || string.isEmpty();
|
|
|
|
}
|
|
|
|
|
2017-03-14 00:44:59 +01:00
|
|
|
public static boolean isEmpty(String string) {
|
|
|
|
return string != null && string.length() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String ensureNotNull(String message) {
|
|
|
|
if (message != null) {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2020-01-29 19:19:53 +01:00
|
|
|
|
|
|
|
public static String terminateNull(String input) {
|
|
|
|
if (input == null || input.length() == 0) {
|
|
|
|
return new String(new byte[]{(byte) 0});
|
|
|
|
}
|
|
|
|
char lastChar = input.charAt(input.length() - 1);
|
|
|
|
if (lastChar == 0) return input;
|
|
|
|
|
|
|
|
byte[] newArray = new byte[input.getBytes().length + 1];
|
|
|
|
System.arraycopy(input.getBytes(), 0, newArray, 0, input.getBytes().length);
|
|
|
|
|
|
|
|
newArray[newArray.length - 1] = 0;
|
|
|
|
|
|
|
|
return new String(newArray);
|
|
|
|
}
|
|
|
|
|
2022-08-18 23:03:28 +02:00
|
|
|
@Nullable
|
|
|
|
public static String untilNullTerminator(final byte[] bytes, final int startOffset) {
|
|
|
|
for (int i = startOffset; i < bytes.length; i++) {
|
|
|
|
if (bytes[i] == 0) {
|
|
|
|
return new String(ArrayUtils.subarray(bytes, startOffset, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-29 19:19:53 +01:00
|
|
|
public static String bytesToHex(byte[] array) {
|
|
|
|
return GB.hexdump(array, 0, -1);
|
|
|
|
}
|
2021-04-20 09:55:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a shortened version of an Android package name by using only the first
|
|
|
|
* character of every non-last part of the package name.
|
|
|
|
* Example: "nodomain.freeyourgadget.gadgetbridge" is shortened to "n.f.gadgetbridge"
|
|
|
|
* @param packageName the original package name
|
|
|
|
* @return the shortened package name
|
|
|
|
*/
|
|
|
|
public static String shortenPackageName(String packageName) {
|
|
|
|
String[] parts = packageName.split("\\.");
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
for (int index=0; index < parts.length; index++) {
|
|
|
|
if (index == parts.length - 1) {
|
|
|
|
result.append(parts[index]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result.append(parts[index].charAt(0)).append(".");
|
|
|
|
}
|
|
|
|
return result.toString();
|
|
|
|
}
|
2017-01-24 02:44:30 +01:00
|
|
|
}
|