File: BinProps.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 (257 lines) | stat: -rw-r--r-- 9,796 bytes parent folder | download | duplicates (2)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//===----------------------------------------------------------------------===//
//
// 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

// WARNING: The values below must be kept in-sync with the stdlib code that
// retrieves these properties. If one should ever update this list below, be
// it reordering bits, adding new properties, etc., please update the
// stdlib code found at:
// 'stdlib/public/core/UnicodeScalarProperties.swift'.
internal struct BinProps: OptionSet {
  let rawValue: UInt64

  init(_ rawValue: UInt64) {
    self.rawValue = rawValue
  }

  // Because we defined the labelless init, we lose the memberwise one
  // generated, so define that here to satisfy the 'OptionSet' requirement.
  init(rawValue: UInt64) {
    self.rawValue = rawValue
  }

  static var changesWhenCaseFolded       : Self { Self(1 &<< 0) }
  static var changesWhenCaseMapped       : Self { Self(1 &<< 1) }
  static var changesWhenLowercased       : Self { Self(1 &<< 2) }
  static var changesWhenNFKCCaseFolded   : Self { Self(1 &<< 3) }
  static var changesWhenTitlecased       : Self { Self(1 &<< 4) }
  static var changesWhenUppercased       : Self { Self(1 &<< 5) }
  static var isASCIIHexDigit             : Self { Self(1 &<< 6) }
  static var isAlphabetic                : Self { Self(1 &<< 7) }
  static var isBidiControl               : Self { Self(1 &<< 8) }
  static var isBidiMirrored              : Self { Self(1 &<< 9) }
  static var isCaseIgnorable             : Self { Self(1 &<< 10) }
  static var isCased                     : Self { Self(1 &<< 11) }
  static var isDash                      : Self { Self(1 &<< 12) }
  static var isDefaultIgnorableCodePoint : Self { Self(1 &<< 13) }
  static var isDeprecated                : Self { Self(1 &<< 14) }
  static var isDiacritic                 : Self { Self(1 &<< 15) }
  static var isEmoji                     : Self { Self(1 &<< 16) }
  static var isEmojiModifier             : Self { Self(1 &<< 17) }
  static var isEmojiModifierBase         : Self { Self(1 &<< 18) }
  static var isEmojiPresentation         : Self { Self(1 &<< 19) }
  static var isExtender                  : Self { Self(1 &<< 20) }
  static var isFullCompositionExclusion  : Self { Self(1 &<< 21) }
  static var isGraphemeBase              : Self { Self(1 &<< 22) }
  static var isGraphemeExtend            : Self { Self(1 &<< 23) }
  static var isHexDigit                  : Self { Self(1 &<< 24) }
  static var isIDContinue                : Self { Self(1 &<< 25) }
  static var isIDSBinaryOperator         : Self { Self(1 &<< 26) }
  static var isIDSTrinaryOperator        : Self { Self(1 &<< 27) }
  static var isIDStart                   : Self { Self(1 &<< 28) }
  static var isIdeographic               : Self { Self(1 &<< 29) }
  static var isJoinControl               : Self { Self(1 &<< 30) }
  static var isLogicalOrderException     : Self { Self(1 &<< 31) }
  static var isLowercase                 : Self { Self(1 &<< 32) }
  static var isMath                      : Self { Self(1 &<< 33) }
  static var isNoncharacterCodePoint     : Self { Self(1 &<< 34) }
  static var isPatternSyntax             : Self { Self(1 &<< 35) }
  static var isPatternWhitespace         : Self { Self(1 &<< 36) }
  static var isQuotationMark             : Self { Self(1 &<< 37) }
  static var isRadical                   : Self { Self(1 &<< 38) }
  static var isSentenceTerminal          : Self { Self(1 &<< 39) }
  static var isSoftDotted                : Self { Self(1 &<< 40) }
  static var isTerminalPunctuation       : Self { Self(1 &<< 41) }
  static var isUnifiedIdeograph          : Self { Self(1 &<< 42) }
  static var isUppercase                 : Self { Self(1 &<< 43) }
  static var isVariationSelector         : Self { Self(1 &<< 44) }
  static var isWhitespace                : Self { Self(1 &<< 45) }
  static var isXIDContinue               : Self { Self(1 &<< 46) }
  static var isXIDStart                  : Self { Self(1 &<< 47) }
}

extension BinProps: Hashable {}

let binPropMappings: [String: BinProps] = [
  "Alphabetic": .isAlphabetic,
  "ASCII_Hex_Digit": .isASCIIHexDigit,
  "Bidi_Control": .isBidiControl,
  "Bidi_Mirrored": .isBidiMirrored,
  "Cased": .isCased,
  "Case_Ignorable": .isCaseIgnorable,
  "Changes_When_Casefolded": .changesWhenCaseFolded,
  "Changes_When_Casemapped": .changesWhenCaseMapped,
  "Changes_When_Lowercased": .changesWhenLowercased,
  "Changes_When_NFKC_Casefolded": .changesWhenNFKCCaseFolded,
  "Changes_When_Titlecased": .changesWhenTitlecased,
  "Changes_When_Uppercased": .changesWhenUppercased,
  "Dash": .isDash,
  "Default_Ignorable_Code_Point": .isDefaultIgnorableCodePoint,
  "Deprecated": .isDeprecated,
  "Diacritic": .isDiacritic,
  "Emoji": .isEmoji,
  "Emoji_Modifier": .isEmojiModifier,
  "Emoji_Modifier_Base": .isEmojiModifierBase,
  "Emoji_Presentation": .isEmojiPresentation,
  "Extender": .isExtender,
  "Full_Composition_Exclusion": .isFullCompositionExclusion,
  "Grapheme_Base": .isGraphemeBase,
  "Grapheme_Extend": .isGraphemeExtend,
  "Hex_Digit": .isHexDigit,
  "ID_Continue": .isIDContinue,
  "ID_Start": .isIDStart,
  "Ideographic": .isIdeographic,
  "IDS_Binary_Operator": .isIDSBinaryOperator,
  "IDS_Trinary_Operator": .isIDSTrinaryOperator,
  "Join_Control": .isJoinControl,
  "Logical_Order_Exception": .isLogicalOrderException,
  "Lowercase": .isLowercase,
  "Math": .isMath,
  "Noncharacter_Code_Point": .isNoncharacterCodePoint,
  "Other_Alphabetic": .isAlphabetic,
  "Other_Default_Ignorable_Code_Point": .isDefaultIgnorableCodePoint,
  "Other_Grapheme_Extend": .isGraphemeExtend,
  "Other_ID_Continue": .isIDContinue,
  "Other_ID_Start": .isIDStart,
  "Other_Lowercase": .isLowercase,
  "Other_Math": .isMath,
  "Other_Uppercase": .isUppercase,
  "Pattern_Syntax": .isPatternSyntax,
  "Pattern_White_Space": .isPatternWhitespace,
  "Quotation_Mark": .isQuotationMark,
  "Radical": .isRadical,
  "Sentence_Terminal": .isSentenceTerminal,
  "Soft_Dotted": .isSoftDotted,
  "Terminal_Punctuation": .isTerminalPunctuation,
  "Unified_Ideograph": .isUnifiedIdeograph,
  "Uppercase": .isUppercase,
  "Variation_Selector": .isVariationSelector,
  "White_Space": .isWhitespace,
  "XID_Continue": .isXIDContinue,
  "XID_Start": .isXIDStart
]

func getBinaryProperties(
  from data: String,
  into result: inout [UInt32: BinProps]
) {
  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: ";")
    
    // Get the property first because we may not care about it.
    let filteredProperty = components[1].filter { !$0.isWhitespace }
    
    guard binPropMappings.keys.contains(filteredProperty) 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
    }
    
    for scalar in scalars {
      result[scalar, default: []].insert(binPropMappings[filteredProperty]!)
    }
  }
}

func emitBinaryProps(
  _ data: [(ClosedRange<UInt32>, BinProps)],
  into result: inout String
) {
  result += """
  #define BIN_PROPS_COUNT \(data.count)
  
  
  """
  
  let combinations = Array(Set(data.map { $0.1 })).map { $0.rawValue }
  
  // Data combinations array
  
  emitCollection(
    combinations,
    name: "_swift_stdlib_scalar_binProps_data",
    into: &result
  )
  
  // Actual scalar + index array
  
  emitCollection(
    data,
    name: "_swift_stdlib_scalar_binProps",
    type: "__swift_uint32_t",
    into: &result
  ) {
    var value = $0.0.lowerBound
    value |= UInt32(combinations.firstIndex(of: $0.1.rawValue)!) << 21
    return "0x\(String(value, radix: 16, uppercase: true))"
  }
}

func generateBinaryProps(for platform: String, into result: inout String) {
  let derivedCoreProps: String
  
  switch platform {
  case "Apple":
    derivedCoreProps = readFile("Data/Apple/DerivedCoreProperties.txt")
  default:
    derivedCoreProps = readFile("Data/DerivedCoreProperties.txt")
  }
  
  let bidiMirrored = readFile("Data/DerivedBinaryProperties.txt")
  let normalization = readFile("Data/DerivedNormalizationProps.txt")
  let emoji = readFile("Data/emoji-data.txt")
  let propList = readFile("Data/PropList.txt")
  
  var binProps: [UInt32: BinProps] = [:]
  getBinaryProperties(from: derivedCoreProps, into: &binProps)
  getBinaryProperties(from: bidiMirrored, into: &binProps)
  getBinaryProperties(from: normalization, into: &binProps)
  getBinaryProperties(from: emoji, into: &binProps)
  getBinaryProperties(from: propList, into: &binProps)
  
  // This loop inserts the ranges of scalars who have no binary properties to
  // fill in the holes for binary search.
  for i in 0x0 ... 0x10FFFF {
    guard let scalar = Unicode.Scalar(i) else {
      continue
    }
    
    if !binProps.keys.contains(scalar.value) {
      binProps[scalar.value] = []
    }
  }
  
  let data = flatten(Array(binProps))
  
  emitBinaryProps(data, into: &result)
}