what-the-bank/sources/com/google/firebase/firestore/local/EncodedPath.java

115 lines
4.0 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.google.firebase.firestore.local;
import com.google.firebase.firestore.model.BasePath;
import com.google.firebase.firestore.model.ResourcePath;
import com.google.firebase.firestore.util.Assert;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class EncodedPath {
EncodedPath() {
}
/* JADX INFO: Access modifiers changed from: package-private */
public static <B extends BasePath<B>> String encode(B b) {
StringBuilder sb = new StringBuilder();
int length = b.length();
for (int i = 0; i < length; i++) {
if (sb.length() > 0) {
encodeSeparator(sb);
}
encodeSegment(b.getSegment(i), sb);
}
encodeSeparator(sb);
return sb.toString();
}
private static void encodeSegment(String str, StringBuilder sb) {
int length = str.length();
for (int i = 0; i < length; i++) {
char charAt = str.charAt(i);
if (charAt == 0) {
sb.append((char) 1);
sb.append((char) 16);
} else if (charAt == 1) {
sb.append((char) 1);
sb.append((char) 17);
} else {
sb.append(charAt);
}
}
}
private static void encodeSeparator(StringBuilder sb) {
sb.append((char) 1);
sb.append((char) 1);
}
/* JADX INFO: Access modifiers changed from: package-private */
public static ResourcePath decodeResourcePath(String str) {
return ResourcePath.fromSegments(decode(str));
}
private static List<String> decode(String str) {
int length = str.length();
boolean z = false;
Assert.hardAssert(length >= 2, "Invalid path \"%s\"", str);
if (length == 2) {
if (str.charAt(0) == 1 && str.charAt(1) == 1) {
z = true;
}
Assert.hardAssert(z, "Non-empty path \"%s\" had length 2", str);
return Collections.emptyList();
}
int length2 = str.length();
ArrayList arrayList = new ArrayList();
StringBuilder sb = new StringBuilder();
int i = 0;
while (i < length) {
int indexOf = str.indexOf(1, i);
if (indexOf < 0 || indexOf > length2 - 2) {
StringBuilder sb2 = new StringBuilder("Invalid encoded resource path: \"");
sb2.append(str);
sb2.append("\"");
throw new IllegalArgumentException(sb2.toString());
}
int i2 = indexOf + 1;
char charAt = str.charAt(i2);
if (charAt == 1) {
String substring = str.substring(i, indexOf);
if (sb.length() != 0) {
sb.append(substring);
substring = sb.toString();
sb.setLength(0);
}
arrayList.add(substring);
} else if (charAt == 16) {
sb.append(str.substring(i, indexOf));
sb.append((char) 0);
} else if (charAt == 17) {
sb.append(str.substring(i, i2));
} else {
StringBuilder sb3 = new StringBuilder("Invalid encoded resource path: \"");
sb3.append(str);
sb3.append("\"");
throw new IllegalArgumentException(sb3.toString());
}
i = indexOf + 2;
}
return arrayList;
}
/* JADX INFO: Access modifiers changed from: package-private */
public static String prefixSuccessor(String str) {
StringBuilder sb = new StringBuilder(str);
int length = sb.length() - 1;
char charAt = sb.charAt(length);
Assert.hardAssert(charAt == 1, "successor may only operate on paths generated by encode", new Object[0]);
sb.setCharAt(length, (char) (charAt + 1));
return sb.toString();
}
}