okhttp

Форк
0
100 строк · 2.8 Кб
1
/*
2
 * Copyright (C) 2023 Square, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
@file:JvmName("GenerateIdnaMappingTableCode")
17

18
package okhttp3.internal.idn
19

20
import com.squareup.kotlinpoet.ClassName
21
import com.squareup.kotlinpoet.FileSpec
22
import com.squareup.kotlinpoet.KModifier
23
import com.squareup.kotlinpoet.PropertySpec
24
import java.io.File
25
import okio.FileSystem
26
import okio.Path.Companion.toPath
27

28
fun main(vararg args: String) {
29
  val data = loadIdnaMappingTableData()
30
  val file = generateMappingTableFile(data)
31
  file.writeTo(File(args[0]))
32
}
33

34
fun loadIdnaMappingTableData(): IdnaMappingTableData {
35
  val path = "/okhttp3/internal/idna/IdnaMappingTable.txt".toPath()
36
  val table =
37
    FileSystem.RESOURCES.read(path) {
38
      readPlainTextIdnaMappingTable()
39
    }
40
  return buildIdnaMappingTableData(table)
41
}
42

43
/**
44
 * Generate a file containing the mapping table's string literals, like this:
45
 *
46
 * ```
47
 * internal val IDNA_MAPPING_TABLE: IdnaMappingTable = IdnaMappingTable(
48
 *   sections = "...",
49
 *   ranges = "...",
50
 *   mappings = "",
51
 * )
52
 * ```
53
 */
54
fun generateMappingTableFile(data: IdnaMappingTableData): FileSpec {
55
  val packageName = "okhttp3.internal.idn"
56
  val idnaMappingTable = ClassName(packageName, "IdnaMappingTable")
57

58
  return FileSpec.builder(packageName, "IdnaMappingTableInstance")
59
    .addProperty(
60
      PropertySpec.builder("IDNA_MAPPING_TABLE", idnaMappingTable)
61
        .addModifiers(KModifier.INTERNAL)
62
        .initializer(
63
          """
64
          |%T(⇥
65
          |sections = "%L",
66
          |ranges = "%L",
67
          |mappings = "%L",
68
          |⇤)
69
          """.trimMargin(),
70
          idnaMappingTable,
71
          data.sections.escapeDataString(),
72
          data.ranges.escapeDataString(),
73
          data.mappings.escapeDataString(),
74
        )
75
        .build(),
76
    )
77
    .build()
78
}
79

80
/**
81
 * KotlinPoet doesn't really know what to do with a string containing NUL, BEL, DEL, etc. We also
82
 * don't want to perform `trimMargin()` at runtime.
83
 */
84
fun String.escapeDataString(): String {
85
  return buildString {
86
    for (codePoint in this@escapeDataString.codePoints()) {
87
      when (codePoint) {
88
        in 0..0x20,
89
        '"'.code,
90
        '$'.code,
91
        '\\'.code,
92
        '·'.code,
93
        127,
94
        -> append(String.format("\\u%04x", codePoint))
95

96
        else -> appendCodePoint(codePoint)
97
      }
98
    }
99
  }
100
}
101

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.