File: Unicode.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 (113 lines) | stat: -rw-r--r-- 3,546 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
//===--- Unicode.swift ----------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-stdlib-swift
// REQUIRES: executable_test

import Swift
import StdlibUnittest


var UnicodeInternals = TestSuite("UnicodeInternals")

UnicodeInternals.test("copy") {
  var u8: [UTF8.CodeUnit] = [ 0, 1, 2, 3, 4, 5 ]
  var u16: [UTF16.CodeUnit] = [ 6, 7, 8, 9, 10, 11 ]

  u16.withUnsafeMutableBufferPointer {
    (u16) -> () in
    let p16 = u16.baseAddress!

    u8.withUnsafeMutableBufferPointer {
      (u8) -> () in
      let p8 = u8.baseAddress!

      UTF16._copy(source: p8, destination: p16, count: 3)
      expectEqual([ 0, 1, 2, 9, 10, 11 ], Array(u16))

      UTF16._copy(source: p16 + 3, destination: p8, count: 3)
      expectEqual([ 9, 10, 11, 3, 4, 5 ], Array(u8))

      UTF16._copy(source: p16, destination: p16 + 3, count: 3)
      expectEqual([ 0, 1, 2, 0, 1, 2 ], Array(u16))

      UTF16._copy(source: p8, destination: p8 + 3, count: 3)
      expectEqual([ 9, 10, 11, 9, 10, 11 ], Array(u8))
    }
  }
}

var UnicodeAPIs = TestSuite("UnicodeAPIs")

UnicodeAPIs.test("UnicodeDecodingResult/Equatable") {
  let instances: [UnicodeDecodingResult] = [
    .scalarValue("a"),
    .scalarValue("b"),
    .emptyInput,
    .error
  ]
  checkEquatable(instances, oracle: ==)
}

typealias ASCII = Unicode.ASCII
typealias UTF8 = Unicode.UTF8
typealias UTF16 = Unicode.UTF16
typealias UTF32 = Unicode.UTF32

UnicodeAPIs.test("UTF-8 and UTF-16 queries") {
  guard #available(SwiftStdlib 5.1, *) else {
    return
  }
  let str = "abรฉร01๐Ÿ˜“๐ŸŽƒ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆใ‚ขใ‚คใ‚ฆใ‚จใ‚ช"
  let scalars = Array(str.unicodeScalars)
  for scalar in scalars {
    expectEqual(String(scalar).utf16.count, UTF16.width(scalar))
    expectEqual(String(scalar).utf8.count, UTF8.width(scalar))

    expectEqual(UTF32.isASCII(scalar.value), UTF8.isASCII(scalar.utf8[0]))
    expectEqual(UTF32.isASCII(scalar.value), ASCII.isASCII(scalar.utf8[0]))
    expectEqual(UTF32.isASCII(scalar.value), UTF16.isASCII(scalar.utf16[0]))

    if scalar.utf16.count == 2 {
      let lead = scalar.utf16[0]
      let trail = scalar.utf16[1]
      expectTrue(UTF16.isLeadSurrogate(lead))
      expectTrue(UTF16.isSurrogate(lead))
      expectFalse(UTF16.isASCII(lead))

      expectTrue(UTF16.isTrailSurrogate(trail))
      expectTrue(UTF16.isSurrogate(trail))
    } else {
      let codeUnit = scalar.utf16[0]
      expectFalse(UTF16.isLeadSurrogate(codeUnit))
      expectFalse(UTF16.isTrailSurrogate(codeUnit))
      expectFalse(UTF16.isSurrogate(codeUnit))

      expectEqual(codeUnit <= 0x7F, UTF16.isASCII(codeUnit))
    }

    expectFalse(UTF8.isContinuation(scalar.utf8[0]))
    if scalar.utf8.count == 1 {
      let ascii = scalar.utf8[0]
      expectFalse(UTF8.isContinuation(ascii))
      expectTrue(UTF8.isASCII(ascii))
    } else {
      expectFalse(UTF8.isASCII(scalar.utf8[0]))
      expectFalse(UTF8.isContinuation(scalar.utf8[0]))
      for i in 1..<scalar.utf8.count {
        expectTrue(UTF8.isContinuation(scalar.utf8[i]))
        expectFalse(UTF8.isASCII(scalar.utf8[i]))
      }
    }
  }
}

runAllTests()