Skip to main content
Code Review

Return to Answer

small clarification
Source Link
Milos
  • 639
  • 5
  • 18

... simply because Character in swift represents extended grapheme clusters, not unicode scalarsutf code points.

... simply because Character in swift represents extended grapheme clusters, not unicode scalars.

... simply because Character in swift represents extended grapheme clusters, not utf code points.

Removing the generic string example for brevity
Source Link
Milos
  • 639
  • 5
  • 18
let precomposed = "\u{D55C}" // 한
let decomposed = "\u{1112}\u{1161}\u{11AB}" // 한
precomposed == decomposed // true
precomposed.characters.indices // 0..<1
decomposed.characters.indices // 0..<3

And in the case of Martin's rangeOfString example:

let s1 = "f🇩🇰oo"
let s2 = "ba🇨🇰r"
let r1 = s1.characters.indices // 0..<7
r1.count // 4
s1.utf16.count // 7
let i = r1.startIndex // 0
i.successor().successor() // 5 (i therefore holds reference to s1!!!)
let precomposed = "\u{D55C}" // 한
let decomposed = "\u{1112}\u{1161}\u{11AB}" // 한
precomposed == decomposed // true
precomposed.characters.indices // 0..<1
decomposed.characters.indices // 0..<3

And in the case of Martin's rangeOfString example:

let s1 = "f🇩🇰oo"
let s2 = "ba🇨🇰r"
let r1 = s1.characters.indices // 0..<7
r1.count // 4
s1.utf16.count // 7
let i = r1.startIndex // 0
i.successor().successor() // 5 (i therefore holds reference to s1!!!)
let s1 = "f🇩🇰oo"
let s2 = "ba🇨🇰r"
let r1 = s1.characters.indices // 0..<7
r1.count // 4
s1.utf16.count // 7
let i = r1.startIndex // 0
i.successor().successor() // 5 (i therefore holds reference to s1!!!)
Showing that String.Index holds reference to "it's" string.
Source Link
Milos
  • 639
  • 5
  • 18
let s1 = "f🇩🇰oo"
let s2 = "ba🇨🇰r"
let r1 = s1.characters.indices // 0..<7
r1.count // 4
s1.utf16.count // 7
r1.startIndex let i = r1.startIndex // 0
r1i.startIndexsuccessor().advancedBysuccessor(2) // 5 (i therefore holds reference //to 5s1!!!)
let s1 = "f🇩🇰oo"
let s2 = "ba🇨🇰r"
let r1 = s1.characters.indices // 0..<7
r1.count // 4
s1.utf16.count // 7
r1.startIndex  // 0
r1.startIndex.advancedBy(2) // 5
let s1 = "f🇩🇰oo"
let s2 = "ba🇨🇰r"
let r1 = s1.characters.indices // 0..<7
r1.count // 4
s1.utf16.count // 7
let i = r1.startIndex // 0
i.successor().successor() // 5 (i therefore holds reference to s1!!!)
minor clarification at the very end
Source Link
Milos
  • 639
  • 5
  • 18
Loading
Appended a discussion on the complexities of String.Index
Source Link
Milos
  • 639
  • 5
  • 18
Loading
Simplified control flow
Source Link
Milos
  • 639
  • 5
  • 18
Loading
Source Link
Milos
  • 639
  • 5
  • 18
Loading
default

AltStyle によって変換されたページ (->オリジナル) /