From bf05f11070181cd6722f734b66cb2d35bc5a08ef Mon Sep 17 00:00:00 2001 From: marc hoffman Date: Fri, 19 Apr 2019 10:05:10 -0400 Subject: [PATCH] String: tweaks/clanup to character view --- Source/String.Views.swift | 36 +++++++----- Source/String.swift | 7 +-- Source/String_Extensions.swift | 56 +------------------ ...s.swift => UnicodeScalar_Extensions.swift} | 0 4 files changed, 27 insertions(+), 72 deletions(-) rename Source/{Char_Extensions.swift => UnicodeScalar_Extensions.swift} (100%) diff --git a/Source/String.Views.swift b/Source/String.Views.swift index 0839783..d9a6ab2 100644 --- a/Source/String.Views.swift +++ b/Source/String.Views.swift @@ -40,20 +40,23 @@ public extension SwiftString { while te.MoveNext() { stringData.append(Character(nativeStringValue: te.Current as! NativeString)) } - #elseif COCOA + #elseif DARWIN var i = 0 while i < length(string) { - let sequenceLength = string.rangeOfComposedCharacterSequenceAtIndex(i).length + let sequenceLength = (string as! NSString).rangeOfComposedCharacterSequenceAtIndex(i).length //76192: Silver: can't use range as subscript? (SBL) let ch: NativeString = string.__substring(range: i ..< i+sequenceLength) stringData.append(Character(nativeStringValue: ch)) i += sequenceLength } + #elseif ISLAND + #hint Not implemented yet #endif - /* old logic to detect surrogate pairs; not needed right now + /* old logic to detect surrogate pairs; not needed right now + { let c = string[i] let c2 = Int(c) /*switch Int(c) { @@ -99,8 +102,8 @@ public extension SwiftString { addCharacter() i += 1 - }*/ - //addCharacter() + } + //addCharacter()*/ } public override var count: Int { return stringData.count } @@ -178,6 +181,13 @@ public extension SwiftString { return stringData[index] } + @Sequence + public func GetSequence() -> ISequence { + for i in startIndex ..< endIndex { + __yield self[i] + } + } + @ToString public func description() -> NativeString { var result = "UTF16CharacterView(" for i in startIndex..*/ { + public class UTF32View: BaseCharacterView { private let stringData: Byte[] private init(stringData: Byte[]) { @@ -207,7 +216,7 @@ public extension SwiftString { #elseif CLR stringData = System.Text.UTF32Encoding(/*bigendian:*/false, /*BOM:*/false).GetBytes(string) // todo check order #elseif ISLAND - stringData = Encoding.UTF32LE.GetBytes(aValue, /*BOM:*/false) // todo check order + stringData = System.Encoding.UTF32LE.GetBytes(string, /*BOM:*/false) // todo check order #elseif COCOA if let utf32 = string.dataUsingEncoding(.NSUTF32LittleEndianStringEncoding) { // todo check order stringData = Byte[](capacity: utf32.length); @@ -263,9 +272,7 @@ public extension SwiftString { return result } } - #endif - #if !ISLAND public class UTF8View: BaseCharacterView { internal let stringData: UTF8Char[] @@ -279,7 +286,7 @@ public extension SwiftString { #elseif CLR stringData = System.Text.UTF8Encoding(/*BOM:*/false).GetBytes(string) #elseif ISLAND - stringData = Encoding.UTF8.GetBytes(aValue, /*BOM:*/false) + stringData = System.Encoding.UTF8.GetBytes(string, /*BOM:*/false) as! UTF8Char[] #elseif COCOA if let utf8 = string.dataUsingEncoding(.NSUTF8StringEncoding) { stringData = UTF8Char[](capacity: utf8.length); @@ -316,6 +323,11 @@ public extension SwiftString { return stringData[index] } + @Sequence + public func GetSequence() -> ISequence { + return stringData + } + @ToString public func description() -> NativeString { var result = "UTF8CharacterView(" for i in startIndex.. NativeString { return nativeStringValue @@ -226,11 +228,9 @@ public struct SwiftString /*: Streamable*/ { #endif } - #if !ISLAND public var utf8: SwiftString.UTF8View { return SwiftString.UTF8View(string: nativeStringValue) } - #endif #if COCOA public var utf8CString: UTF8Char[] { @@ -246,11 +246,9 @@ public struct SwiftString /*: Streamable*/ { return SwiftString.UTF16View(string: nativeStringValue) } - #if !ISLAND public var unicodeScalars: SwiftString.UnicodeScalarView { return SwiftString.UnicodeScalarView(string: nativeStringValue) } - #endif // // Methods @@ -432,5 +430,4 @@ public struct SwiftString /*: Streamable*/ { return nil #endif } - } \ No newline at end of file diff --git a/Source/String_Extensions.swift b/Source/String_Extensions.swift index 66a903e..243187c 100644 --- a/Source/String_Extensions.swift +++ b/Source/String_Extensions.swift @@ -76,9 +76,11 @@ // Properties // + #if DARWIN || !ISLAND public var characters: SwiftString.CharacterView { return SwiftString.CharacterView(string: self) } + #endif #if !COCOA public var debugDescription: NativeString { @@ -184,21 +186,17 @@ #endif } - #if !ISLAND public var utf8: SwiftString.UTF8View { return SwiftString.UTF8View(string: self) } - #endif public var utf16: SwiftString.UTF16View { return SwiftString.UTF16View(string: self) } - #if !ISLAND public var unicodeScalars: SwiftString.UnicodeScalarView { return SwiftString.UnicodeScalarView(string: self) } - #endif // // Methods @@ -335,54 +333,4 @@ return nil #endif } - - public __abstract class CharacterView { - /*fileprivate*/internal init(string: NativeString) { - } - - public var startIndex: NativeString.Index { return 0 } - public __abstract var endIndex: NativeString.Index { get } - - } - - public class UTF16CharacterView: CharacterView, ICustomDebugStringConvertible { - private let string: NativeString - - /*fileprivate*/internal init(string: NativeString) { - self.string = string - } - - public override var endIndex: NativeString.Index { return length(string) } - - public subscript(index: Int) -> UTF16Char { - return string[index] - } - - #if COCOA - override var debugDescription: NativeString! { - var result = "UTF16CharacterView(" - for i in startIndex.. startIndex { - result += " " - } - result += UInt64(self[i]).toHexString(length: 4) - } - result += ")" - return result - } - #else - public var debugDescription: NativeString { - var result = "UTF16CharacterView(" - for i in startIndex.. startIndex { - result += " " - } - result += UInt64(self[i]).toHexString(length: 4) - } - result += ")" - return result - } - #endif - } - } \ No newline at end of file diff --git a/Source/Char_Extensions.swift b/Source/UnicodeScalar_Extensions.swift similarity index 100% rename from Source/Char_Extensions.swift rename to Source/UnicodeScalar_Extensions.swift