I tried to implement the Boyer-Moore algorithm in the Swift Playground, and I used the Swift String.Index a lot, and something that bothered me was why the indexes are stored 4 times as much as it seems. >
For instance:
let why = "is s on 4th position not 1st".index(of: "s")
This code in the Swift Playground _compoundOffset 4
does not generate 1. I am sure there is a reason for this, but I could not find an explanation anywhere.
This is not a duplicate of any question that explains how to get the char index in Swift, I know that I used the index (of :) function to illustrate the question. I wanted to know why the 2nd char value is 4 not 1 when using String.Index.
So, I believe that it keeps the indices private, and I don’t need to know the internal implementation, maybe this is due to the UTF16 and UTF32 encodings.
source
share