[swift-evolution] Strings in Swift 4
david at alkaline-solutions.com
Sat Feb 25 00:26:50 CST 2017
It might have helped if instead of being called String and Character, they were named Text and ExtendedGraphemeCluster.
They don’t really have the same behavior or functionality as string/characters in many other languages, especially older languages. This is because in many languages, strings are not just text but also random-accesss (possibly binary) data.
Thats not to say that there aren’t a ton of algorithms where you can use Text like a String, treat ExtendedGraphemeCluster like a character, and get unicode behavior without thinking about it.
But when it comes to random access and/or byte modification, you are better off working with something closer to a traditional (byte) string interface.
Trying to wedge random access and byte modification into the Swift String will simply complicate everything, slow down the algorithms which don’t need it, eat up more memory, as well as slow down bridging between Swift and Objective C code.
Hence me suggesting earlier working with Data, [UInt8], or [Character] within the context of your manipulation code, then converting to a Swift String at the end. Convert to the data format you need, then convert back.
Thats not to say that there aren’t features which would simplify/clarify algorithms working in this manner.
> On Feb 24, 2017, at 4:27 PM, Ted F.A. van Gaalen via swift-evolution <swift-evolution at swift.org> wrote:
> ok, I understand, thank you
>> On 25 Feb 2017, at 00:25, David Sweeris <davesweeris at mac.com <mailto:davesweeris at mac.com>> wrote:
>>> On Feb 24, 2017, at 13:41, Ted F.A. van Gaalen <tedvgiosdev at gmail.com <mailto:tedvgiosdev at gmail.com>> wrote:
>>> Hi David & Dave
>>> can you explain that in more detail?
>>>>> Wouldn’t that turn simple character access into a mutating function?
>>> assigning like s[11…14] = str is of course, yes.
>>> only then - that is if the character array thus has been changed -
>>> it has to update the string in storage, yes.
>>> but str = s[n..<m] doesn’t. mutate.
>>> so you’d have to maintain keep (private) a isChanged: Bool or bit.
>>> a checksum over the character array .
>> It mutates because the String has to instantiate the Array<Character> to which you're indexing into, if it doesn't already exist. It may not make any externally visible changes, but it's still a change.
>> - Dave Sweeris
> swift-evolution mailing list
> swift-evolution at swift.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution