Skip to content

Commit

Permalink
String: tweaks/clanup to character view
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarfland committed Apr 19, 2019
1 parent 823d486 commit bf05f11
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 72 deletions.
36 changes: 23 additions & 13 deletions Source/String.Views.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -99,8 +102,8 @@ public extension SwiftString {
addCharacter()

i += 1
}*/
//addCharacter()
}
//addCharacter()*/
}

public override var count: Int { return stringData.count }
Expand Down Expand Up @@ -178,6 +181,13 @@ public extension SwiftString {
return stringData[index]
}

@Sequence
public func GetSequence() -> ISequence<Char> {
for i in startIndex ..< endIndex {
__yield self[i]
}
}

@ToString public func description() -> NativeString {
var result = "UTF16CharacterView("
for i in startIndex..<endIndex {
Expand All @@ -191,10 +201,9 @@ public extension SwiftString {
}
}

#if !ISLAND
public typealias UnicodeScalarView = UTF32View

public class UTF32View: BaseCharacterView/*, ISequence<UTF32Char>*/ {
public class UTF32View: BaseCharacterView {
private let stringData: Byte[]

private init(stringData: Byte[]) {
Expand All @@ -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);
Expand Down Expand Up @@ -263,9 +272,7 @@ public extension SwiftString {
return result
}
}
#endif

#if !ISLAND
public class UTF8View: BaseCharacterView {
internal let stringData: UTF8Char[]

Expand All @@ -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);
Expand Down Expand Up @@ -316,6 +323,11 @@ public extension SwiftString {
return stringData[index]
}

@Sequence
public func GetSequence() -> ISequence<UTF8Char> {
return stringData
}

@ToString public func description() -> NativeString {
var result = "UTF8CharacterView("
for i in startIndex..<endIndex {
Expand All @@ -328,6 +340,4 @@ public extension SwiftString {
return result
}
}
#endif

}
7 changes: 2 additions & 5 deletions Source/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ public struct SwiftString /*: Streamable*/ {
// Properties
//

#if DARWIN || !ISLAND
public var characters: SwiftString.CharacterView {
return SwiftString.CharacterView(string: nativeStringValue)
}
#endif

@ToString public func description() -> NativeString {
return nativeStringValue
Expand Down Expand Up @@ -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[] {
Expand All @@ -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
Expand Down Expand Up @@ -432,5 +430,4 @@ public struct SwiftString /*: Streamable*/ {
return nil
#endif
}

}
56 changes: 2 additions & 54 deletions Source/String_Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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..<endIndex {
if i > startIndex {
result += " "
}
result += UInt64(self[i]).toHexString(length: 4)
}
result += ")"
return result
}
#else
public var debugDescription: NativeString {
var result = "UTF16CharacterView("
for i in startIndex..<endIndex {
if i > startIndex {
result += " "
}
result += UInt64(self[i]).toHexString(length: 4)
}
result += ")"
return result
}
#endif
}

}
File renamed without changes.

0 comments on commit bf05f11

Please sign in to comment.