let s = "abcacd"
let nextA = s.dropFirst().firstIndex(of: "a")!
s[..<nextA]
which is how I thought you were supposed to do this kind of thing (getting indices of / doing searches on subsections of a string for use with the whole string) and is the whole reason we don't have API that takes ranges for operation everywhere
If you're not supposed to do this kind of thing, what should you do instead?
Note still that SubSequence
will share index with the corresponding Collection
.
So a valid ArraySlice
index will also be valid for corresponding Array
; and a valid Array
index will be a valid for corresponding ArraySlice
if it’s in the range for example.
That’s how we make slicing collections very cheap.
Lantua:
Why would this fail? AFAICT the assigner and assignee of value type should be indistinguishable from one another.
As explained in the previous thread, to implement index invalidation detection, a future version of String.Index
could mix in bits of the address. Therefore, two bitwise identical values could nonetheless have incompatible indices. (Now, String
is a copy-on-write type, so the address could remain the same, but IIUC, there’s no guarantee of such.)
TellowKrinkle:
For that last one, wouldn't that include usage of substring indices on their original strings
Substrings share indices with their parent strings.
zwaldowski:
NSString
isn’t strictly needed to be modern, either. In particular, you may want to look at this initializer to work with NSRange
. We may also be able to help better if you want to give a more specific example of what you’re trying to modernize!
I'm calling a Rust api and getting back items. Each item has a title that comes in as utf8 formatted bytes and highlight ranges that come in as utf8 offset pairs that represent ranges in that text.
I will display these items in a NSTableView with the ranges highlighted, so my end target is to get the utf8 bytes into a NSAttributedString and get the offsets into NSRanges so that I can add highlight ranges to the attributed string.
Right now I'm converting the rust data over to these structs:
struct Item {
let text: String
let highlights: [NSRange]
And they are the "Items" I return from my NSOutlineView data source. It's working fine as is, but I thought it would be more swift style to use Swift's native Range instead of NSRange... so that's why I was trying to store the highlights as [Range<String.Index>] and ran into above described problems.
Anyway, I'm happy enough using NSRange for storage... it's what I need in the end to build up the NSAttributedString anyway.
Jesse_Grosjean:
Each item has a title that comes in as utf8 formatted bytes and highlight ranges that come in as utf8 offset pairs that represent ranges in that text.
You need to be dead sure no middleman between the two APIs is treating the communications as text and possibly subjecting it to normalization. Otherwise you need to be prepared for the UTF‐8 offsets to be incorrect or even out of bounds.