Skip to content

Commit 8240f0b

Browse files
authored
Merge pull request #61548 from lorentey/string-index-printing
2 parents 9ad0f98 + 704b565 commit 8240f0b

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

stdlib/public/core/StringIndex.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,54 @@ extension String.Index: Hashable {
512512
hasher.combine(orderingValue)
513513
}
514514
}
515+
516+
extension String.Index {
517+
@_alwaysEmitIntoClient
518+
internal var _encodingDescription: String {
519+
switch (_rawBits & Self.__utf8Bit != 0, _rawBits & Self.__utf16Bit != 0) {
520+
case (false, false): return "unknown"
521+
case (true, false): return "utf8"
522+
case (false, true): return "utf16"
523+
case (true, true): return "any"
524+
}
525+
}
526+
527+
// The definitions below are placeholders for potential future `String.Index`
528+
// conformances to `CustomStringConvertible` and
529+
// `CustomDebugStringConvertible`. They are supplied here to make working with
530+
// string indices somewhat bearable while we're working on adding the actual
531+
// conformances.
532+
533+
/// A textual representation of this instance.
534+
@_alwaysEmitIntoClient
535+
@inline(never)
536+
public var _description: String {
537+
// 23[utf8]+1
538+
var d = "\(_encodedOffset)[\(_encodingDescription)]"
539+
if transcodedOffset != 0 {
540+
d += "+\(transcodedOffset)"
541+
}
542+
return d
543+
}
544+
545+
/// A textual representation of this instance, suitable for debugging.
546+
@_alwaysEmitIntoClient
547+
@inline(never)
548+
public var _debugDescription: String {
549+
var d = "String.Index("
550+
d += "offset: \(_encodedOffset)[\(_encodingDescription)]"
551+
if transcodedOffset != 0 {
552+
d += "+\(transcodedOffset)"
553+
}
554+
if _isCharacterAligned {
555+
d += ", aligned: character"
556+
} else if _isScalarAligned {
557+
d += ", aligned: scalar"
558+
}
559+
if let stride = characterStride {
560+
d += ", stride: \(stride)"
561+
}
562+
d += ")"
563+
return d
564+
}
565+
}

0 commit comments

Comments
 (0)