Skip to content

Commit eadef7a

Browse files
committed
[stdlib] String.Index: Use symbolic names rather than magic constants
1 parent b06e6e5 commit eadef7a

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

stdlib/public/core/StringIndex.swift

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,26 @@ extension String.Index {
248248
}
249249
}
250250

251+
extension String.Index {
252+
@_alwaysEmitIntoClient @inline(__always) // Swift 5.7
253+
internal static var __scalarAlignmentBit: UInt64 { 0x1 }
254+
255+
@_alwaysEmitIntoClient @inline(__always) // Swift 5.7
256+
internal static var __characterAlignmentBit: UInt64 { 0x2 }
257+
258+
@_alwaysEmitIntoClient @inline(__always) // Swift 5.7
259+
internal static var __utf8Bit: UInt64 { 0x4 }
260+
261+
@_alwaysEmitIntoClient @inline(__always) // Swift 5.7
262+
internal static var __utf16Bit: UInt64 { 0x8 }
263+
264+
@_alwaysEmitIntoClient @inline(__always) // Swift 5.7
265+
internal static func __encodingBit(utf16: Bool) -> UInt64 {
266+
let utf16 = Int8(Builtin.zext_Int1_Int8(utf16._value))
267+
return __utf8Bit &<< utf16
268+
}
269+
}
270+
251271
/*
252272
Index Scalar Alignment
253273

@@ -289,13 +309,15 @@ extension String.Index {
289309
extension String.Index {
290310
@_alwaysEmitIntoClient // Swift 5.1
291311
@inline(__always)
292-
internal var _isScalarAligned: Bool { return 0 != _rawBits & 0x1 }
312+
internal var _isScalarAligned: Bool {
313+
0 != _rawBits & Self.__scalarAlignmentBit
314+
}
293315

294316
@_alwaysEmitIntoClient // Swift 5.1
295317
@inline(__always)
296318
internal var _scalarAligned: String.Index {
297319
var idx = self
298-
idx._rawBits |= 0x1
320+
idx._rawBits |= Self.__scalarAlignmentBit
299321
idx._invariantCheck()
300322
return idx
301323
}
@@ -325,7 +347,9 @@ extension String.Index {
325347
extension String.Index {
326348
@_alwaysEmitIntoClient // Swift 5.7
327349
@inline(__always)
328-
internal var _isCharacterAligned: Bool { return 0 != _rawBits & 0x2 }
350+
internal var _isCharacterAligned: Bool {
351+
0 != _rawBits & Self.__characterAlignmentBit
352+
}
329353

330354
/// Return the same index with both the scalar- and `Character`-aligned bits
331355
/// set.
@@ -334,7 +358,8 @@ extension String.Index {
334358
@_alwaysEmitIntoClient // Swift 5.7
335359
@inline(__always)
336360
internal var _characterAligned: String.Index {
337-
let idx = Self(_rawBits | 0x3)
361+
let r = _rawBits | Self.__characterAlignmentBit | Self.__scalarAlignmentBit
362+
let idx = Self(r)
338363
idx._invariantCheck()
339364
return idx
340365
}
@@ -385,6 +410,12 @@ extension String.Index {
385410
// handled in `_StringGuts.ensureMatchingEncoding(_:)`; see there for the sordid
386411
// details.
387412
extension String.Index {
413+
@_alwaysEmitIntoClient // Swift 5.7
414+
@inline(__always)
415+
internal var _encodingBits: UInt64 {
416+
_rawBits & (Self.__utf8Bit | Self.__utf16Bit)
417+
}
418+
388419
/// Returns true if the position in this index can be interpreted as an offset
389420
/// into UTF-8-encoded string storage.
390421
///
@@ -394,7 +425,7 @@ extension String.Index {
394425
@inline(__always)
395426
internal var _canBeUTF8: Bool {
396427
// The only way an index cannot be UTF-8 is it has only the UTF-16 flag set.
397-
_rawBits & 0xC != 0x08
428+
_encodingBits != Self.__utf16Bit
398429
}
399430

400431
/// Returns true if the position in this index can be interpreted as offset
@@ -407,45 +438,30 @@ extension String.Index {
407438
@inline(__always)
408439
internal var _canBeUTF16: Bool {
409440
// The only way an index cannot be UTF-16 is it has only the UTF-8 flag set.
410-
_rawBits & 0xC != 0x04
441+
_encodingBits != Self.__utf8Bit
411442
}
412443

413444
/// Returns the same index with the UTF-8 bit set.
414445
@_alwaysEmitIntoClient // Swift 5.7
415446
@inline(__always)
416-
internal var _knownUTF8: Self { Self(_rawBits | 0x4) }
447+
internal var _knownUTF8: Self { Self(_rawBits | Self.__utf8Bit) }
417448

418449
/// Returns the same index with the UTF-16 bit set.
419450
@_alwaysEmitIntoClient // Swift 5.7
420451
@inline(__always)
421-
internal var _knownUTF16: Self { Self(_rawBits | 0x8) }
452+
internal var _knownUTF16: Self { Self(_rawBits | Self.__utf16Bit) }
422453

423454
/// Returns the same index with both UTF-8 & UTF-16 bits set.
424455
@_alwaysEmitIntoClient // Swift 5.7
425456
@inline(__always)
426-
internal var _encodingIndependent: Self { Self(_rawBits | 0xC) }
427-
428-
/// Returns true if the UTF-8 flag is set.
429-
///
430-
/// This is for debugging purposes only. Do not use this property to determine
431-
/// whether an index is compatible with UTF-8 storage; instead, use
432-
/// `_canBeUTF8`.
433-
@_alwaysEmitIntoClient // Swift 5.7
434-
@inline(__always)
435-
internal var __isUTF8: Bool { _rawBits & 0x4 != 0 }
436-
437-
/// Returns true if the UTF-16 flag is set.
438-
///
439-
/// This is for debugging purposes only. Do not use this property to determine
440-
/// whether an index is compatible with UTF-16 storage; instead, use
441-
/// `_canBeUTF16`.
442-
@_alwaysEmitIntoClient // Swift 5.7
443-
@inline(__always)
444-
internal var __isUTF16: Bool { _rawBits & 0x8 != 0 }
457+
internal var _encodingIndependent: Self {
458+
Self(_rawBits | Self.__utf8Bit | Self.__utf16Bit)
459+
}
445460

446461
@_alwaysEmitIntoClient // Swift 5.7
447462
internal func _copyEncoding(from index: Self) -> Self {
448-
Self((_rawBits & ~0xC) | (index._rawBits & 0xC))
463+
let mask = Self.__utf8Bit | Self.__utf16Bit
464+
return Self((_rawBits & ~mask) | (index._rawBits & mask))
449465
}
450466
}
451467

@@ -487,7 +503,7 @@ extension String.Index {
487503
}
488504

489505
d += ", encoding: "
490-
switch (__isUTF8, __isUTF16) {
506+
switch (_rawBits & Self.__utf8Bit != 0, _rawBits & Self.__utf16Bit != 0) {
491507
case (false, false): d += "unknown"
492508
case (true, false): d += "utf8"
493509
case (false, true): d += "utf16"

0 commit comments

Comments
 (0)