package com.fasterxml.jackson.core.io;

import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import com.fasterxml.jackson.core.util.TextBuffer;
import com.google.firebase.perf.util.Constants;
import java.util.Arrays;

/* loaded from: classes.dex */
public final class JsonStringEncoder {
    private static final int INITIAL_BYTE_BUFFER_SIZE = 200;
    private static final int INITIAL_CHAR_BUFFER_SIZE = 120;
    private static final int SURR1_FIRST = 55296;
    private static final int SURR1_LAST = 56319;
    private static final int SURR2_FIRST = 56320;
    private static final int SURR2_LAST = 57343;
    private static final char[] HC = CharTypes.copyHexChars();
    private static final byte[] HB = CharTypes.copyHexBytes();
    private static final JsonStringEncoder instance = new JsonStringEncoder();

    private int _appendByte(int i10, int i11, ByteArrayBuilder byteArrayBuilder, int i12) {
        byteArrayBuilder.setCurrentSegmentLength(i12);
        byteArrayBuilder.append(92);
        if (i11 < 0) {
            byteArrayBuilder.append(117);
            if (i10 > 255) {
                int i13 = i10 >> 8;
                byte[] bArr = HB;
                byteArrayBuilder.append(bArr[i13 >> 4]);
                byteArrayBuilder.append(bArr[i13 & 15]);
                i10 &= Constants.MAX_HOST_LENGTH;
            } else {
                byteArrayBuilder.append(48);
                byteArrayBuilder.append(48);
            }
            byte[] bArr2 = HB;
            byteArrayBuilder.append(bArr2[i10 >> 4]);
            byteArrayBuilder.append(bArr2[i10 & 15]);
        } else {
            byteArrayBuilder.append((byte) i11);
        }
        return byteArrayBuilder.getCurrentSegmentLength();
    }

    private int _appendNamed(int i10, char[] cArr) {
        cArr[1] = (char) i10;
        return 2;
    }

    private int _appendNumeric(int i10, char[] cArr) {
        cArr[1] = 'u';
        char[] cArr2 = HC;
        cArr[4] = cArr2[i10 >> 4];
        cArr[5] = cArr2[i10 & 15];
        return 6;
    }

    private static int _convert(int i10, int i11) {
        if (i11 >= 56320 && i11 <= 57343) {
            return ((i10 - 55296) << 10) + 65536 + (i11 - 56320);
        }
        throw new IllegalArgumentException("Broken surrogate pair: first char 0x" + Integer.toHexString(i10) + ", second 0x" + Integer.toHexString(i11) + "; illegal combination");
    }

    private static void _illegal(int i10) {
        throw new IllegalArgumentException(UTF8Writer.illegalSurrogateDesc(i10));
    }

    private char[] _qbuf() {
        return new char[]{'\\', 0, '0', '0'};
    }

    public static JsonStringEncoder getInstance() {
        return instance;
    }

    /* JADX WARN: Removed duplicated region for block: B:31:0x00e0  */
    /* JADX WARN: Removed duplicated region for block: B:34:0x00e9 A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public byte[] encodeAsUTF8(String str) {
        int i10;
        int i11;
        int length = str.length();
        int i12 = INITIAL_BYTE_BUFFER_SIZE;
        byte[] bArr = new byte[INITIAL_BYTE_BUFFER_SIZE];
        ByteArrayBuilder byteArrayBuilder = null;
        int i13 = 0;
        int i14 = 0;
        loop0: while (true) {
            if (i13 >= length) {
                break;
            }
            int i15 = i13 + 1;
            int charAt = str.charAt(i13);
            while (charAt <= 127) {
                if (i14 >= i12) {
                    if (byteArrayBuilder == null) {
                        byteArrayBuilder = ByteArrayBuilder.fromInitial(bArr, i14);
                    }
                    byte[] finishCurrentSegment = byteArrayBuilder.finishCurrentSegment();
                    i14 = 0;
                    bArr = finishCurrentSegment;
                    i12 = finishCurrentSegment.length;
                }
                int i16 = i14 + 1;
                bArr[i14] = (byte) charAt;
                if (i15 >= length) {
                    i14 = i16;
                    break loop0;
                }
                int charAt2 = str.charAt(i15);
                i15++;
                charAt = charAt2;
                i14 = i16;
            }
            if (byteArrayBuilder == null) {
                byteArrayBuilder = ByteArrayBuilder.fromInitial(bArr, i14);
            }
            if (i14 >= i12) {
                bArr = byteArrayBuilder.finishCurrentSegment();
                i12 = bArr.length;
                i14 = 0;
            }
            if (charAt < 2048) {
                i10 = i14 + 1;
                bArr[i14] = (byte) ((charAt >> 6) | 192);
            } else if (charAt >= 55296 && charAt <= 57343) {
                if (charAt > 56319) {
                    _illegal(charAt);
                }
                if (i15 >= length) {
                    _illegal(charAt);
                }
                int i17 = i15 + 1;
                int _convert = _convert(charAt, str.charAt(i15));
                if (_convert > 1114111) {
                    _illegal(_convert);
                }
                int i18 = i14 + 1;
                bArr[i14] = (byte) ((_convert >> 18) | 240);
                if (i18 >= i12) {
                    bArr = byteArrayBuilder.finishCurrentSegment();
                    i12 = bArr.length;
                    i18 = 0;
                }
                int i19 = i18 + 1;
                bArr[i18] = (byte) (((_convert >> 12) & 63) | 128);
                if (i19 >= i12) {
                    byte[] finishCurrentSegment2 = byteArrayBuilder.finishCurrentSegment();
                    i19 = 0;
                    bArr = finishCurrentSegment2;
                    i12 = finishCurrentSegment2.length;
                }
                int i20 = i19 + 1;
                bArr[i19] = (byte) (((_convert >> 6) & 63) | 128);
                i11 = _convert;
                i13 = i17;
                i10 = i20;
                if (i10 < i12) {
                    byte[] finishCurrentSegment3 = byteArrayBuilder.finishCurrentSegment();
                    i10 = 0;
                    bArr = finishCurrentSegment3;
                    i12 = finishCurrentSegment3.length;
                }
                bArr[i10] = (byte) ((i11 & 63) | 128);
                i14 = i10 + 1;
            } else {
                int i21 = i14 + 1;
                bArr[i14] = (byte) ((charAt >> 12) | 224);
                if (i21 >= i12) {
                    byte[] finishCurrentSegment4 = byteArrayBuilder.finishCurrentSegment();
                    i21 = 0;
                    bArr = finishCurrentSegment4;
                    i12 = finishCurrentSegment4.length;
                }
                bArr[i21] = (byte) (((charAt >> 6) & 63) | 128);
                i10 = i21 + 1;
            }
            i11 = charAt;
            i13 = i15;
            if (i10 < i12) {
            }
            bArr[i10] = (byte) ((i11 & 63) | 128);
            i14 = i10 + 1;
        }
        if (byteArrayBuilder == null) {
            return Arrays.copyOfRange(bArr, 0, i14);
        }
        return byteArrayBuilder.completeAndCoalesce(i14);
    }

    /* JADX WARN: Code restructure failed: missing block: B:10:0x0024, code lost:
    
        r9 = r7 + 1;
        r7 = r13.charAt(r7);
        r10 = r1[r7];
     */
    /* JADX WARN: Code restructure failed: missing block: B:11:0x002c, code lost:
    
        if (r10 >= 0) goto L13;
     */
    /* JADX WARN: Code restructure failed: missing block: B:12:0x002e, code lost:
    
        r7 = _appendNumeric(r7, r6);
     */
    /* JADX WARN: Code restructure failed: missing block: B:13:0x0037, code lost:
    
        r10 = r8 + r7;
     */
    /* JADX WARN: Code restructure failed: missing block: B:14:0x003a, code lost:
    
        if (r10 <= r0.length) goto L22;
     */
    /* JADX WARN: Code restructure failed: missing block: B:15:0x003c, code lost:
    
        r10 = r0.length - r8;
     */
    /* JADX WARN: Code restructure failed: missing block: B:16:0x003e, code lost:
    
        if (r10 <= 0) goto L19;
     */
    /* JADX WARN: Code restructure failed: missing block: B:17:0x0040, code lost:
    
        java.lang.System.arraycopy(r6, 0, r0, r8, r10);
     */
    /* JADX WARN: Code restructure failed: missing block: B:18:0x0043, code lost:
    
        if (r5 != null) goto L21;
     */
    /* JADX WARN: Code restructure failed: missing block: B:19:0x0045, code lost:
    
        r5 = com.fasterxml.jackson.core.util.TextBuffer.fromInitial(r0);
     */
    /* JADX WARN: Code restructure failed: missing block: B:20:0x0049, code lost:
    
        r0 = r5.finishCurrentSegment();
        r7 = r7 - r10;
        java.lang.System.arraycopy(r6, r10, r0, 0, r7);
        r8 = r7;
     */
    /* JADX WARN: Code restructure failed: missing block: B:23:0x0053, code lost:
    
        java.lang.System.arraycopy(r6, 0, r0, r8, r7);
        r8 = r10;
     */
    /* JADX WARN: Code restructure failed: missing block: B:25:0x0033, code lost:
    
        r7 = _appendNamed(r10, r6);
     */
    /* JADX WARN: Code restructure failed: missing block: B:8:0x001e, code lost:
    
        if (r6 != null) goto L10;
     */
    /* JADX WARN: Code restructure failed: missing block: B:9:0x0020, code lost:
    
        r6 = _qbuf();
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public char[] quoteAsString(String str) {
        int i10;
        char[] cArr = new char[120];
        int[] iArr = CharTypes.get7BitOutputEscapes();
        int length = iArr.length;
        int length2 = str.length();
        TextBuffer textBuffer = null;
        int i11 = 0;
        int i12 = 0;
        char[] cArr2 = null;
        loop0: while (true) {
            if (i11 >= length2) {
                break;
            }
            while (true) {
                char charAt = str.charAt(i11);
                if (charAt < length && iArr[charAt] != 0) {
                    break;
                }
                if (i12 >= cArr.length) {
                    if (textBuffer == null) {
                        textBuffer = TextBuffer.fromInitial(cArr);
                    }
                    cArr = textBuffer.finishCurrentSegment();
                    i12 = 0;
                }
                int i13 = i12 + 1;
                cArr[i12] = charAt;
                i11++;
                if (i11 >= length2) {
                    i12 = i13;
                    break loop0;
                }
                i12 = i13;
            }
            i11 = i10;
        }
        if (textBuffer == null) {
            return Arrays.copyOfRange(cArr, 0, i12);
        }
        textBuffer.setCurrentLength(i12);
        return textBuffer.contentsAsArray();
    }

    /* JADX WARN: Removed duplicated region for block: B:39:0x00f9  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public byte[] quoteAsUTF8(String str) {
        int i10;
        int i11;
        int i12;
        int length = str.length();
        byte[] bArr = new byte[INITIAL_BYTE_BUFFER_SIZE];
        ByteArrayBuilder byteArrayBuilder = null;
        int i13 = 0;
        int i14 = 0;
        loop0: while (true) {
            if (i13 >= length) {
                break;
            }
            int[] iArr = CharTypes.get7BitOutputEscapes();
            while (true) {
                char charAt = str.charAt(i13);
                if (charAt > 127 || iArr[charAt] != 0) {
                    break;
                }
                if (i14 >= bArr.length) {
                    if (byteArrayBuilder == null) {
                        byteArrayBuilder = ByteArrayBuilder.fromInitial(bArr, i14);
                    }
                    bArr = byteArrayBuilder.finishCurrentSegment();
                    i14 = 0;
                }
                int i15 = i14 + 1;
                bArr[i14] = (byte) charAt;
                i13++;
                if (i13 >= length) {
                    i14 = i15;
                    break loop0;
                }
                i14 = i15;
            }
            if (byteArrayBuilder == null) {
                byteArrayBuilder = ByteArrayBuilder.fromInitial(bArr, i14);
            }
            if (i14 >= bArr.length) {
                bArr = byteArrayBuilder.finishCurrentSegment();
                i14 = 0;
            }
            int i16 = i13 + 1;
            char charAt2 = str.charAt(i13);
            if (charAt2 <= 127) {
                i14 = _appendByte(charAt2, iArr[charAt2], byteArrayBuilder, i14);
                bArr = byteArrayBuilder.getCurrentSegment();
                i13 = i16;
            } else {
                if (charAt2 <= 2047) {
                    i11 = i14 + 1;
                    bArr[i14] = (byte) ((charAt2 >> 6) | 192);
                    i10 = (charAt2 & '?') | 128;
                } else if (charAt2 < 55296 || charAt2 > 57343) {
                    int i17 = i14 + 1;
                    bArr[i14] = (byte) ((charAt2 >> '\f') | 224);
                    if (i17 >= bArr.length) {
                        bArr = byteArrayBuilder.finishCurrentSegment();
                        i17 = 0;
                    }
                    bArr[i17] = (byte) (((charAt2 >> 6) & 63) | 128);
                    i10 = (charAt2 & '?') | 128;
                    i11 = i17 + 1;
                } else {
                    if (charAt2 > 56319) {
                        _illegal(charAt2);
                    }
                    if (i16 >= length) {
                        _illegal(charAt2);
                    }
                    int i18 = i16 + 1;
                    int _convert = _convert(charAt2, str.charAt(i16));
                    if (_convert > 1114111) {
                        _illegal(_convert);
                    }
                    int i19 = i14 + 1;
                    bArr[i14] = (byte) ((_convert >> 18) | 240);
                    if (i19 >= bArr.length) {
                        bArr = byteArrayBuilder.finishCurrentSegment();
                        i19 = 0;
                    }
                    int i20 = i19 + 1;
                    bArr[i19] = (byte) (((_convert >> 12) & 63) | 128);
                    if (i20 >= bArr.length) {
                        bArr = byteArrayBuilder.finishCurrentSegment();
                        i20 = 0;
                    }
                    int i21 = i20 + 1;
                    bArr[i20] = (byte) (((_convert >> 6) & 63) | 128);
                    i12 = (_convert & 63) | 128;
                    i13 = i18;
                    i11 = i21;
                    if (i11 >= bArr.length) {
                        bArr = byteArrayBuilder.finishCurrentSegment();
                        i11 = 0;
                    }
                    bArr[i11] = (byte) i12;
                    i14 = i11 + 1;
                }
                i12 = i10;
                i13 = i16;
                if (i11 >= bArr.length) {
                }
                bArr[i11] = (byte) i12;
                i14 = i11 + 1;
            }
        }
        return byteArrayBuilder == null ? Arrays.copyOfRange(bArr, 0, i14) : byteArrayBuilder.completeAndCoalesce(i14);
    }

    /* JADX WARN: Code restructure failed: missing block: B:13:0x0029, code lost:
    
        if (r6 != null) goto L14;
     */
    /* JADX WARN: Code restructure failed: missing block: B:14:0x002b, code lost:
    
        r6 = _qbuf();
     */
    /* JADX WARN: Code restructure failed: missing block: B:15:0x002f, code lost:
    
        r9 = r7 + 1;
        r7 = r13.charAt(r7);
        r10 = r1[r7];
     */
    /* JADX WARN: Code restructure failed: missing block: B:16:0x0037, code lost:
    
        if (r10 >= 0) goto L17;
     */
    /* JADX WARN: Code restructure failed: missing block: B:17:0x0039, code lost:
    
        r7 = _appendNumeric(r7, r6);
     */
    /* JADX WARN: Code restructure failed: missing block: B:18:0x0042, code lost:
    
        r10 = r8 + r7;
     */
    /* JADX WARN: Code restructure failed: missing block: B:19:0x0045, code lost:
    
        if (r10 <= r0.length) goto L26;
     */
    /* JADX WARN: Code restructure failed: missing block: B:20:0x0047, code lost:
    
        r10 = r0.length - r8;
     */
    /* JADX WARN: Code restructure failed: missing block: B:21:0x0049, code lost:
    
        if (r10 <= 0) goto L23;
     */
    /* JADX WARN: Code restructure failed: missing block: B:22:0x004b, code lost:
    
        java.lang.System.arraycopy(r6, 0, r0, r8, r10);
     */
    /* JADX WARN: Code restructure failed: missing block: B:23:0x004e, code lost:
    
        if (r4 != null) goto L25;
     */
    /* JADX WARN: Code restructure failed: missing block: B:24:0x0050, code lost:
    
        r4 = com.fasterxml.jackson.core.util.TextBuffer.fromInitial(r0);
     */
    /* JADX WARN: Code restructure failed: missing block: B:25:0x0054, code lost:
    
        r0 = r4.finishCurrentSegment();
        r7 = r7 - r10;
        java.lang.System.arraycopy(r6, r10, r0, 0, r7);
        r8 = r7;
     */
    /* JADX WARN: Code restructure failed: missing block: B:28:0x005e, code lost:
    
        java.lang.System.arraycopy(r6, 0, r0, r8, r7);
        r8 = r10;
     */
    /* JADX WARN: Code restructure failed: missing block: B:30:0x003e, code lost:
    
        r7 = _appendNamed(r10, r6);
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public char[] quoteAsString(CharSequence charSequence) {
        int i10;
        if (charSequence instanceof String) {
            return quoteAsString((String) charSequence);
        }
        char[] cArr = new char[120];
        int[] iArr = CharTypes.get7BitOutputEscapes();
        int length = iArr.length;
        int length2 = charSequence.length();
        TextBuffer textBuffer = null;
        char[] cArr2 = null;
        int i11 = 0;
        int i12 = 0;
        loop0: while (true) {
            if (i11 >= length2) {
                break;
            }
            while (true) {
                char charAt = charSequence.charAt(i11);
                if (charAt < length && iArr[charAt] != 0) {
                    break;
                }
                if (i12 >= cArr.length) {
                    if (textBuffer == null) {
                        textBuffer = TextBuffer.fromInitial(cArr);
                    }
                    cArr = textBuffer.finishCurrentSegment();
                    i12 = 0;
                }
                int i13 = i12 + 1;
                cArr[i12] = charAt;
                i11++;
                if (i11 >= length2) {
                    i12 = i13;
                    break loop0;
                }
                i12 = i13;
            }
            i11 = i10;
        }
        if (textBuffer == null) {
            return Arrays.copyOfRange(cArr, 0, i12);
        }
        textBuffer.setCurrentLength(i12);
        return textBuffer.contentsAsArray();
    }

    /* JADX WARN: Removed duplicated region for block: B:31:0x00e0  */
    /* JADX WARN: Removed duplicated region for block: B:34:0x00e9 A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public byte[] encodeAsUTF8(CharSequence charSequence) {
        int i10;
        int i11;
        int length = charSequence.length();
        int i12 = INITIAL_BYTE_BUFFER_SIZE;
        byte[] bArr = new byte[INITIAL_BYTE_BUFFER_SIZE];
        ByteArrayBuilder byteArrayBuilder = null;
        int i13 = 0;
        int i14 = 0;
        loop0: while (true) {
            if (i13 >= length) {
                break;
            }
            int i15 = i13 + 1;
            int charAt = charSequence.charAt(i13);
            while (charAt <= 127) {
                if (i14 >= i12) {
                    if (byteArrayBuilder == null) {
                        byteArrayBuilder = ByteArrayBuilder.fromInitial(bArr, i14);
                    }
                    byte[] finishCurrentSegment = byteArrayBuilder.finishCurrentSegment();
                    i14 = 0;
                    bArr = finishCurrentSegment;
                    i12 = finishCurrentSegment.length;
                }
                int i16 = i14 + 1;
                bArr[i14] = (byte) charAt;
                if (i15 >= length) {
                    i14 = i16;
                    break loop0;
                }
                int charAt2 = charSequence.charAt(i15);
                i15++;
                charAt = charAt2;
                i14 = i16;
            }
            if (byteArrayBuilder == null) {
                byteArrayBuilder = ByteArrayBuilder.fromInitial(bArr, i14);
            }
            if (i14 >= i12) {
                bArr = byteArrayBuilder.finishCurrentSegment();
                i12 = bArr.length;
                i14 = 0;
            }
            if (charAt < 2048) {
                i10 = i14 + 1;
                bArr[i14] = (byte) ((charAt >> 6) | 192);
            } else if (charAt >= 55296 && charAt <= 57343) {
                if (charAt > 56319) {
                    _illegal(charAt);
                }
                if (i15 >= length) {
                    _illegal(charAt);
                }
                int i17 = i15 + 1;
                int _convert = _convert(charAt, charSequence.charAt(i15));
                if (_convert > 1114111) {
                    _illegal(_convert);
                }
                int i18 = i14 + 1;
                bArr[i14] = (byte) ((_convert >> 18) | 240);
                if (i18 >= i12) {
                    bArr = byteArrayBuilder.finishCurrentSegment();
                    i12 = bArr.length;
                    i18 = 0;
                }
                int i19 = i18 + 1;
                bArr[i18] = (byte) (((_convert >> 12) & 63) | 128);
                if (i19 >= i12) {
                    byte[] finishCurrentSegment2 = byteArrayBuilder.finishCurrentSegment();
                    i19 = 0;
                    bArr = finishCurrentSegment2;
                    i12 = finishCurrentSegment2.length;
                }
                int i20 = i19 + 1;
                bArr[i19] = (byte) (((_convert >> 6) & 63) | 128);
                i11 = _convert;
                i13 = i17;
                i10 = i20;
                if (i10 < i12) {
                    byte[] finishCurrentSegment3 = byteArrayBuilder.finishCurrentSegment();
                    i10 = 0;
                    bArr = finishCurrentSegment3;
                    i12 = finishCurrentSegment3.length;
                }
                bArr[i10] = (byte) ((i11 & 63) | 128);
                i14 = i10 + 1;
            } else {
                int i21 = i14 + 1;
                bArr[i14] = (byte) ((charAt >> 12) | 224);
                if (i21 >= i12) {
                    byte[] finishCurrentSegment4 = byteArrayBuilder.finishCurrentSegment();
                    i21 = 0;
                    bArr = finishCurrentSegment4;
                    i12 = finishCurrentSegment4.length;
                }
                bArr[i21] = (byte) (((charAt >> 6) & 63) | 128);
                i10 = i21 + 1;
            }
            i11 = charAt;
            i13 = i15;
            if (i10 < i12) {
            }
            bArr[i10] = (byte) ((i11 & 63) | 128);
            i14 = i10 + 1;
        }
        if (byteArrayBuilder == null) {
            return Arrays.copyOfRange(bArr, 0, i14);
        }
        return byteArrayBuilder.completeAndCoalesce(i14);
    }

    public void quoteAsString(CharSequence charSequence, StringBuilder sb2) {
        int _appendNamed;
        int[] iArr = CharTypes.get7BitOutputEscapes();
        int length = iArr.length;
        int length2 = charSequence.length();
        char[] cArr = null;
        int i10 = 0;
        while (i10 < length2) {
            do {
                char charAt = charSequence.charAt(i10);
                if (charAt < length && iArr[charAt] != 0) {
                    if (cArr == null) {
                        cArr = _qbuf();
                    }
                    int i11 = i10 + 1;
                    char charAt2 = charSequence.charAt(i10);
                    int i12 = iArr[charAt2];
                    if (i12 < 0) {
                        _appendNamed = _appendNumeric(charAt2, cArr);
                    } else {
                        _appendNamed = _appendNamed(i12, cArr);
                    }
                    sb2.append(cArr, 0, _appendNamed);
                    i10 = i11;
                } else {
                    sb2.append(charAt);
                    i10++;
                }
            } while (i10 < length2);
            return;
        }
    }
}
