Skip to content

Commit 7407c0a

Browse files
committed
Some more String APIs and related protocols
1 parent 9c77359 commit 7407c0a

File tree

3 files changed

+72
-31
lines changed

3 files changed

+72
-31
lines changed

Source/Protocols.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ public protocol IStreamable {
4646
func writeTo(_ target: OutputStreamType) // deliberately different that Apple's SBL due to limitations on Island
4747
}
4848

49+
public typealias TextOutputStream = ITextOutputStream
50+
public protocol ITextOutputStream {
51+
mutating func write(_ string: String)
52+
}
53+
54+
public typealias TextOutputStreamable = ITextOutputStreamable
55+
public protocol ITextOutputStreamable {
56+
//func write<Target>(to target: inout Target) where Target : TextOutputStream
57+
func write(to target: inout TextOutputStream)
58+
}
59+
60+
public typealias StringProtocol = IStringProtocol
61+
public protocol IStringProtocol : ITextOutputStream {
62+
}
63+
4964
//
5065
// Collections, Sequences and the like
5166
//

Source/String.swift

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -368,37 +368,7 @@ public struct SwiftString /*: Streamable*/ {
368368
//
369369

370370
func split(_ separator: String) -> [String] {
371-
372-
let separatorLength = separator.length()
373-
if separatorLength == 0 {
374-
return [self]
375-
}
376-
377-
#if COOPER
378-
//exit nativeString.split(java.util.regex.Pattern.quote(Separator)) as not nullable
379-
//Custom implementation because `mapped.split` strips empty oparts at the end, making it incomopatible with the other three platfroms.
380-
var result = [String]()
381-
var i = 0
382-
while true {
383-
let p = nativeStringValue.indexOf(separator, i)
384-
if p > -1 {
385-
let part = nativeStringValue.substring(i, p-i)
386-
result.append(part)
387-
i = p+separatorLength
388-
} else {
389-
let part = nativeStringValue.substring(i)
390-
result.append(part)
391-
break
392-
}
393-
}
394-
return result
395-
#elseif ECHOES
396-
return [String](nativeStringValue.Split([separator], StringSplitOptions.None).ToList())
397-
#elseif ISLAND
398-
return [String](nativeStringValue.Split(separator).ToList())
399-
#elseif TOFFEE
400-
return [String](nativeStringValue.componentsSeparatedByString(separator))
401-
#endif
371+
return nativeStringValue.components(separatedBy: separator)
402372
}
403373

404374
//

Source/String_Extensions.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,25 @@
3535
}
3636
}
3737

38+
public convenience init(describing subject: Object) {
39+
/*if let instance = subject as? ITextOutputStreamable {
40+
instance.write(to: )
41+
} else*/ if let instance = subject as? ICustomStringConvertible {
42+
return instance.description
43+
} else if let instance = subject as? CustomDebugStringConvertible {
44+
return instance.debugDescription
45+
} else {
46+
return init(subject)
47+
}
48+
}
49+
3850
public convenience init(reflecting subject: Object) {
3951
if let o = subject as? ICustomDebugStringConvertible {
4052
return o.debugDescription
53+
} else if let instance = subject as? ICustomStringConvertible {
54+
return instance.description
55+
//} else if let instance = subject as? ITextOutputStreamable {
56+
//instance.write(to: )
4157
} else {
4258
#if JAVA
4359
// ToDo: fall back to reflection to call debugDescription?
@@ -112,6 +128,46 @@
112128
return 0
113129
}
114130

131+
//public var capitalized: NativeString {
132+
////return uppercaseString
133+
//}
134+
135+
//func components(separatedBy separator: CharacterSet) -> [String] {
136+
//}
137+
138+
func components(separatedBy separator: String) -> [String] {
139+
let separatorLength = separator.length()
140+
if separatorLength == 0 {
141+
return [self]
142+
}
143+
144+
#if COOPER
145+
//exit nativeString.split(java.util.regex.Pattern.quote(Separator)) as not nullable
146+
//Custom implementation because `mapped.split` strips empty parts at the end, making it incompatible with the other three platfroms.
147+
var result = [String]()
148+
var i = 0
149+
while true {
150+
let p = indexOf(separator, i)
151+
if p > -1 {
152+
let part = substring(i, p-i)
153+
result.append(part)
154+
i = p+separatorLength
155+
} else {
156+
let part = substring(i)
157+
result.append(part)
158+
break
159+
}
160+
}
161+
return result
162+
#elseif ECHOES
163+
return [String](Split([separator], StringSplitOptions.None).ToList())
164+
#elseif ISLAND
165+
return [String](Split(separator).ToList())
166+
#elseif TOFFEE
167+
return [String](componentsSeparatedByString(separator))
168+
#endif
169+
}
170+
115171
#if !COCOA
116172
public var uppercaseString: NativeString {
117173
#if JAVA

0 commit comments

Comments
 (0)