[swift-evolution] Strings in Swift 4

Ronald Bell captainzones at yahoo.com
Sat Feb 11 09:59:34 CST 2017


> On Feb 10, 2017, at 12:38 PM, Hooman Mehr via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On Feb 9, 2017, at 6:50 PM, Shawn Erickson <shawnce at gmail.com <mailto: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)
>>>> TedvG
>>> 
>>> 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?
>> 
>> -Shawn
> 
> 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.

On that last topic, NSAttributedString has always seemed like a strange design — a class with a bunch of attribute methods with a property that lets you interrogate the String behind it all. 

It always seemed to me that there was a false distinction. Strings should have optional properties, the way GameplayKit does GKEntities and GKComponents.

Simple Strings would return nil if you asked them for their attributes.

Attributed Strings would return attributes.

I think it would be a lot more intuitive how to parse an attributed string in blocks and then refer back to the attributes of each chunk, for one thing.

Is there a reason why composition was chosen to be the way it is in NSAttributedString, instead?

- Ron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170211/111402fa/attachment.html>


More information about the swift-evolution mailing list