Files
AzooKeyKanaKanjiConverter/Sources/SwiftUtils/StringUtils.swift
2023-07-23 00:34:27 +09:00

118 lines
4.5 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// extension StringProtocol.swift
// Keyboard
//
// Created by ensan on 2020/10/16.
// Copyright © 2020 ensan. All rights reserved.
//
import Foundation
public extension StringProtocol {
///
/// - note: `false`
@inlinable
var onlyRomanAlphabetOrNumber: Bool {
!isEmpty && range(of: "[^a-zA-Z0-9]", options: .regularExpression) == nil
}
///
/// - note: `false`
@inlinable
var onlyRomanAlphabet: Bool {
!isEmpty && range(of: "[^a-zA-Z]", options: .regularExpression) == nil
}
///
/// - note: `false`
/// 40
@inlinable
var containsRomanAlphabet: Bool {
for value in self.utf8 {
if (UInt8(ascii: "a") <= value && value <= UInt8(ascii: "z")) || (UInt8(ascii: "A") <= value && value <= UInt8(ascii: "Z")) {
return true
}
}
return false
}
///
/// - note: `false`
@inlinable
var isEnglishSentence: Bool {
!isEmpty && range(of: "[^0-9a-zA-Z\n !'_<>\\[\\]{}*@`\\^|~=\"#$%&\\+\\(\\),\\-\\./:;?\\\\]", options: .regularExpression) == nil
}
///
@inlinable
var isKana: Bool {
!isEmpty && range(of: "[^ぁ-ゖァ-ヶ]", options: .regularExpression) == nil
}
/// Returns a String value in which Hiraganas are all converted to Katakana.
/// - Returns: A String value in which Hiraganas are all converted to Katakana.
@inlinable func toKatakana() -> String {
// utf162utf16
let result = self.utf16.map { scalar -> UInt16 in
if 0x3041 <= scalar && scalar <= 0x3096 {
return scalar + 96
} else {
return scalar
}
}
return String(utf16CodeUnits: result, count: result.count)
}
/// Returns a String value in which Katakana are all converted to Hiragana.
/// - Returns: A String value in which Katakana are all converted to Hiragana.
@inlinable func toHiragana() -> String {
// utf162utf16
let result = self.utf16.map { scalar -> UInt16 in
if 0x30A1 <= scalar && scalar <= 0x30F6 {
return scalar - 96
} else {
return scalar
}
}
return String(utf16CodeUnits: result, count: result.count)
}
/// Returns an Index value that is the specified distance from the start index.
/// - Parameter:
/// - offset: The distance to offset from the start index.
/// - Returns: An Index value that is the specified distance from the start index.
@inlinable
func indexFromStart(_ offset: Int) -> Index {
self.index(self.startIndex, offsetBy: offset)
}
// :
/*
\ -> \\
\0 -> \0
\n -> \n
\t -> \t
, -> \c
" -> \d
*/
// please use these letters in order to avoid user-inputting text crash
func escaped() -> String {
var result = self.replacingOccurrences(of: "\\", with: "\\b")
result = result.replacingOccurrences(of: "\0", with: "\\0")
result = result.replacingOccurrences(of: "\n", with: "\\n")
result = result.replacingOccurrences(of: "\t", with: "\\t")
result = result.replacingOccurrences(of: ",", with: "\\c")
result = result.replacingOccurrences(of: " ", with: "\\s")
result = result.replacingOccurrences(of: "\"", with: "\\d")
return result
}
func unescaped() -> String {
var result = self.replacingOccurrences(of: "\\d", with: "\"")
result = result.replacingOccurrences(of: "\\s", with: " ")
result = result.replacingOccurrences(of: "\\c", with: ",")
result = result.replacingOccurrences(of: "\\t", with: "\t")
result = result.replacingOccurrences(of: "\\n", with: "\n")
result = result.replacingOccurrences(of: "\\0", with: "\0")
result = result.replacingOccurrences(of: "\\b", with: "\\")
return result
}
}