|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +// Normalization tests are currently only avaible on Darwin, awaiting a sensible |
| 14 | +// file API... |
| 15 | +#if _runtime(_ObjC) |
| 16 | +import Foundation |
| 17 | + |
| 18 | +func parseGraphemeBreakTests( |
| 19 | + _ data: String, |
| 20 | + into result: inout [(String, Int)] |
| 21 | +) { |
| 22 | + for line in data.split(separator: "\n") { |
| 23 | + // Only look at actual tests |
| 24 | + guard line.hasPrefix("÷") else { |
| 25 | + continue |
| 26 | + } |
| 27 | + |
| 28 | + let info = line.split(separator: "#") |
| 29 | + let components = info[0].split(separator: " ") |
| 30 | + |
| 31 | + var string = "" |
| 32 | + var count = 0 |
| 33 | + |
| 34 | + for i in components.indices { |
| 35 | + guard i != 0 else { |
| 36 | + continue |
| 37 | + } |
| 38 | + |
| 39 | + let scalar: Unicode.Scalar |
| 40 | + |
| 41 | + // If we're an odd index, this is a scalar. |
| 42 | + if i & 0x1 == 1 { |
| 43 | + scalar = Unicode.Scalar(UInt32(components[i], radix: 16)!)! |
| 44 | + |
| 45 | + string.unicodeScalars.append(scalar) |
| 46 | + } else { |
| 47 | + // Otherwise, it is a grapheme breaking operator. |
| 48 | + |
| 49 | + // If this is a break, record the +1 count. Otherwise it is × which is |
| 50 | + // not a break. |
| 51 | + if components[i] == "÷" { |
| 52 | + count += 1 |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + result.append((string, count)) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +public let graphemeBreakTests: [(String, Int)] = { |
| 62 | + var result: [(String, Int)] = [] |
| 63 | + |
| 64 | + let testFile = readInputFile("GraphemeBreakTest.txt") |
| 65 | + |
| 66 | + parseGraphemeBreakTests(testFile, into: &result) |
| 67 | + |
| 68 | + return result |
| 69 | +}() |
| 70 | +#endif |
0 commit comments