1 /* 2 * Copyright 2017-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.serialization.json.internal 6 7 import kotlin.native.concurrent.* 8 toHexCharnull9private fun toHexChar(i: Int) : Char { 10 val d = i and 0xf 11 return if (d < 10) (d + '0'.code).toChar() 12 else (d - 10 + 'a'.code).toChar() 13 } 14 <lambda>null15internal val ESCAPE_STRINGS: Array<String?> = arrayOfNulls<String>(93).apply { 16 for (c in 0..0x1f) { 17 val c1 = toHexChar(c shr 12) 18 val c2 = toHexChar(c shr 8) 19 val c3 = toHexChar(c shr 4) 20 val c4 = toHexChar(c) 21 this[c] = "\\u$c1$c2$c3$c4" 22 } 23 this['"'.code] = "\\\"" 24 this['\\'.code] = "\\\\" 25 this['\t'.code] = "\\t" 26 this['\b'.code] = "\\b" 27 this['\n'.code] = "\\n" 28 this['\r'.code] = "\\r" 29 this[0x0c] = "\\f" 30 } 31 <lambda>null32internal val ESCAPE_MARKERS: ByteArray = ByteArray(93).apply { 33 for (c in 0..0x1f) { 34 this[c] = 1.toByte() 35 } 36 this['"'.code] = '"'.code.toByte() 37 this['\\'.code] = '\\'.code.toByte() 38 this['\t'.code] = 't'.code.toByte() 39 this['\b'.code] = 'b'.code.toByte() 40 this['\n'.code] = 'n'.code.toByte() 41 this['\r'.code] = 'r'.code.toByte() 42 this[0x0c] = 'f'.code.toByte() 43 } 44 printQuotednull45internal fun StringBuilder.printQuoted(value: String) { 46 append(STRING) 47 var lastPos = 0 48 for (i in value.indices) { 49 val c = value[i].code 50 if (c < ESCAPE_STRINGS.size && ESCAPE_STRINGS[c] != null) { 51 append(value, lastPos, i) // flush prev 52 append(ESCAPE_STRINGS[c]) 53 lastPos = i + 1 54 } 55 } 56 57 if (lastPos != 0) append(value, lastPos, value.length) 58 else append(value) 59 append(STRING) 60 } 61 62 /** 63 * Returns `true` if the contents of this string is equal to the word "true", ignoring case, `false` if content equals "false", 64 * and returns `null` otherwise. 65 */ toBooleanStrictOrNullnull66internal fun String.toBooleanStrictOrNull(): Boolean? = when { 67 this.equals("true", ignoreCase = true) -> true 68 this.equals("false", ignoreCase = true) -> false 69 else -> null 70 } 71