[swift-evolution] Strings in Swift 4
hooman at mac.com
Fri Feb 10 12:38:31 CST 2017
> On Feb 9, 2017, at 6:50 PM, Shawn Erickson <shawnce at gmail.com> wrote:
> On Thu, Feb 9, 2017 at 3:45 PM Hooman Mehr <hooman at mac.com <mailto:hooman at mac.com>> wrote:
>> On Feb 9, 2017, at 3:11 PM, Dave Abrahams <dabrahams at apple.com <mailto:dabrahams at apple.com>> wrote:
>> on Thu Feb 09 2017, "Ted F.A. van Gaalen" <tedvgiosdev-AT-gmail.com <http://tedvgiosdev-at-gmail.com/>> wrote:
>>> Hello Shawn
>>> Just google with any programming language name and “string manipulation”
>>> and you have enough reading for a week or so :o)
>> That truly doesn't answer the question. It's not, “why do people index
>> strings with integers when that's the only tool they are given for
>> decomposing strings?” It's, “what do you have to do with strings that's
>> hard in Swift *because* you can't index them with integers?”
> I have done some string processing. I have not encountered any algorithm where an integer index is absolutely needed, but sometimes it might be the most convenient.
> For example, there are valid reasons to keep side tables that hold indexes into a string. (such as maintaining attributes that apply to a substring or things like pre-computed positions of soft line breaks). It does not require the index to be integer, but maintaining validity of those indexes after the string is mutated requires being able to offset them back or forth from some position on. These operations could be less verbose and easier if the index happens to be integer or (efficiently) supports + - operators. Also, I know there are other methods to deal with such things and mutating a large string generally is a bad idea, but sometimes it is the easiest and most convenient solution to the problem at hand.
> The end goal of this string is for human consumption right? So such manipulation would need need to unicode aware in the modern world? ..or is it for some other reason?
For an example of what I mean, see the source code of NS(Mutable)AttributedString <https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSAttributedString.swift> and note how most of the mutating methods of Mutable variant are not implemented yet...
So, a good example of where such indexing would be convenient, could be writing a swift-native AttributedString backed by Swift native String.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution