Improved printable string

pull/214/head
M66B 11 months ago
parent d95aebfcc0
commit 95aed14c59

@ -232,7 +232,7 @@ public class AdapterMedia extends RecyclerView.Adapter<AdapterMedia.ViewHolder>
BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader();
Result result = reader.decode(bBitmap);
args.putString("barcode_text", Helper.getPrintableString(result.getText()));
args.putString("barcode_text", Helper.getPrintableString(result.getText(), false));
args.putString("barcode_format", result.getBarcodeFormat().name());
} catch (NotFoundException ex) {
Log.w(ex);

@ -2177,28 +2177,29 @@ public class Helper {
return df.format(sign * bytes / Math.pow(unit, exp)) + " " + pre + "B";
}
static boolean isPrintableChar(char c) {
Character.UnicodeBlock block = Character.UnicodeBlock.of(c);
static boolean isPrintableChar(int codepoint) {
Character.UnicodeBlock block = Character.UnicodeBlock.of(codepoint);
if (block == null || block == Character.UnicodeBlock.SPECIALS)
return false;
return !Character.isISOControl(c);
return !Character.isISOControl(codepoint);
}
// https://issuetracker.google.com/issues/37054851
static String getPrintableString(String value) {
static String getPrintableString(String value, boolean debug) {
if (TextUtils.isEmpty(value))
return value;
StringBuilder result = new StringBuilder();
for (int i = 0; i < value.length(); i++) {
char kar = value.charAt(i);
if (kar == '\n')
for (int i = 0; i < value.length(); ) {
int codepoint = value.codePointAt(i);
if (debug && codepoint == 10)
result.append('|');
else if (kar == ' ')
else if (debug && codepoint == 32)
result.append('_');
else if (!Helper.isPrintableChar(kar) || kar == '\u00a0')
result.append('{').append(Integer.toHexString(kar)).append('}');
else if (!Helper.isPrintableChar(codepoint) || codepoint == 160)
result.append('{').append(Integer.toHexString(codepoint)).append('}');
else
result.append(kar);
result.append(Character.toChars(codepoint));
i += Character.charCount(codepoint);
}
return result.toString();
}

@ -625,9 +625,9 @@ public class Log {
if (element instanceof Long)
elements[i] = element + " (0x" + Long.toHexString((Long) element) + ")";
else if (element instanceof Spanned)
elements[i] = "(span:" + Helper.getPrintableString(element.toString()) + ")";
elements[i] = "(span:" + Helper.getPrintableString(element.toString(), true) + ")";
else
elements[i] = (element == null ? "<null>" : Helper.getPrintableString(element.toString()));
elements[i] = (element == null ? "<null>" : Helper.getPrintableString(element.toString(), true));
}
value = TextUtils.join(",", elements);
if (length > 10)
@ -636,7 +636,7 @@ public class Log {
} else if (v instanceof Long)
value = v + " (0x" + Long.toHexString((Long) v) + ")";
else if (v instanceof Spanned)
value = "(span:" + Helper.getPrintableString(v.toString()) + ")";
value = "(span:" + Helper.getPrintableString(v.toString(), true) + ")";
else if (v instanceof Bundle)
value = "{" + TextUtils.join(" ", getExtras((Bundle) v)) + "}";

Loading…
Cancel
Save