File: GeneralCategory.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (182 lines) | stat: -rw-r--r-- 4,599 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import GenUtils

enum GeneralCategory: String {
  case uppercaseLetter = "Lu"
  case lowercaseLetter = "Ll"
  case titlecaseLetter = "Lt"
  case modifierLetter = "Lm"
  case otherLetter = "Lo"
  case nonspacingMark = "Mn"
  case spacingMark = "Mc"
  case enclosingMark = "Me"
  case decimalNumber = "Nd"
  case letterNumber = "Nl"
  case otherNumber = "No"
  case connectorPunctuation = "Pc"
  case dashPunctuation = "Pd"
  case openPunctuation = "Ps"
  case closePunctuation = "Pe"
  case initialPunctuation = "Pi"
  case finalPunctuation = "Pf"
  case otherPunctuation = "Po"
  case mathSymbol = "Sm"
  case currencySymbol = "Sc"
  case modifierSymbol = "Sk"
  case otherSymbol = "So"
  case spaceSeparator = "Zs"
  case lineSeparator = "Zl"
  case paragraphSeparator = "Zp"
  case control = "Cc"
  case format = "Cf"
  case surrogate = "Cs"
  case privateUse = "Co"
  case unassigned = "Cn"
  
  var binaryRepresentation: UInt8 {
    switch self {
    case .uppercaseLetter:
      return 0
    case .lowercaseLetter:
      return 1
    case .titlecaseLetter:
      return 2
    case .modifierLetter:
      return 3
    case .otherLetter:
      return 4
    case .nonspacingMark:
      return 5
    case .spacingMark:
      return 6
    case .enclosingMark:
      return 7
    case .decimalNumber:
      return 8
    case .letterNumber:
      return 9
    case .otherNumber:
      return 10
    case .connectorPunctuation:
      return 11
    case .dashPunctuation:
      return 12
    case .openPunctuation:
      return 13
    case .closePunctuation:
      return 14
    case .initialPunctuation:
      return 15
    case .finalPunctuation:
      return 16
    case .otherPunctuation:
      return 17
    case .mathSymbol:
      return 18
    case .currencySymbol:
      return 19
    case .modifierSymbol:
      return 20
    case .otherSymbol:
      return 21
    case .spaceSeparator:
      return 22
    case .lineSeparator:
      return 23
    case .paragraphSeparator:
      return 24
    case .control:
      return 25
    case .format:
      return 26
    case .surrogate:
      return 27
    case .privateUse:
      return 28
    case .unassigned:
      return 29
    }
  }
}

func getGeneralCategory(
  from data: String,
  into result: inout [(ClosedRange<UInt32>, GeneralCategory)]
) {
  for line in data.split(separator: "\n") {
    // Skip comments
    guard !line.hasPrefix("#") else {
      continue
    }
    
    let info = line.split(separator: "#")
    let components = info[0].split(separator: ";")
    
    let filteredCategory = components[1].filter { !$0.isWhitespace }
    
    guard let gc = GeneralCategory(rawValue: filteredCategory) else {
      continue
    }
    
    let scalars: ClosedRange<UInt32>
    
    let filteredScalars = components[0].filter { !$0.isWhitespace }
    
    // If we have . appear, it means we have a legitimate range. Otherwise,
    // it's a singular scalar.
    if filteredScalars.contains(".") {
      let range = filteredScalars.split(separator: ".")
      
      scalars = UInt32(range[0], radix: 16)! ... UInt32(range[1], radix: 16)!
    } else {
      let scalar = UInt32(filteredScalars, radix: 16)!
      
      scalars = scalar ... scalar
    }
    
    result.append((scalars, gc))
  }
}

func emitGeneralCategory(
  _ data: [(ClosedRange<UInt32>, GeneralCategory)],
  into result: inout String
) {
  emitCollection(
    data,
    name: "_swift_stdlib_generalCategory",
    type: "__swift_uint64_t",
    into: &result
  ) {
    var value: UInt64 = UInt64($0.0.lowerBound)
    
    let generalCategory = $0.1.binaryRepresentation
    
    value |= UInt64(generalCategory) << 21
    
    value |= UInt64($0.0.count - 1) << 32
    
    return "0x\(String(value, radix: 16, uppercase: true))"
  }
}

func generateGeneralCategory(into result: inout String) {
  let derivedGeneralCategory = readFile("Data/DerivedGeneralCategory.txt")
  
  var data: [(ClosedRange<UInt32>, GeneralCategory)] = []
  
  getGeneralCategory(from: derivedGeneralCategory, into: &data)
  
  emitGeneralCategory(flatten(data), into: &result)
}