@@ -248,6 +248,26 @@ extension String.Index {
248
248
}
249
249
}
250
250
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
+
251
271
/*
252
272
Index Scalar Alignment
253
273
@@ -289,13 +309,15 @@ extension String.Index {
289
309
extension String . Index {
290
310
@_alwaysEmitIntoClient // Swift 5.1
291
311
@inline ( __always)
292
- internal var _isScalarAligned : Bool { return 0 != _rawBits & 0x1 }
312
+ internal var _isScalarAligned : Bool {
313
+ 0 != _rawBits & Self . __scalarAlignmentBit
314
+ }
293
315
294
316
@_alwaysEmitIntoClient // Swift 5.1
295
317
@inline ( __always)
296
318
internal var _scalarAligned : String . Index {
297
319
var idx = self
298
- idx. _rawBits |= 0x1
320
+ idx. _rawBits |= Self . __scalarAlignmentBit
299
321
idx. _invariantCheck ( )
300
322
return idx
301
323
}
@@ -325,7 +347,9 @@ extension String.Index {
325
347
extension String . Index {
326
348
@_alwaysEmitIntoClient // Swift 5.7
327
349
@inline ( __always)
328
- internal var _isCharacterAligned : Bool { return 0 != _rawBits & 0x2 }
350
+ internal var _isCharacterAligned : Bool {
351
+ 0 != _rawBits & Self . __characterAlignmentBit
352
+ }
329
353
330
354
/// Return the same index with both the scalar- and `Character`-aligned bits
331
355
/// set.
@@ -334,7 +358,8 @@ extension String.Index {
334
358
@_alwaysEmitIntoClient // Swift 5.7
335
359
@inline ( __always)
336
360
internal var _characterAligned : String . Index {
337
- let idx = Self ( _rawBits | 0x3 )
361
+ let r = _rawBits | Self . __characterAlignmentBit | Self . __scalarAlignmentBit
362
+ let idx = Self ( r)
338
363
idx. _invariantCheck ( )
339
364
return idx
340
365
}
@@ -385,6 +410,12 @@ extension String.Index {
385
410
// handled in `_StringGuts.ensureMatchingEncoding(_:)`; see there for the sordid
386
411
// details.
387
412
extension String . Index {
413
+ @_alwaysEmitIntoClient // Swift 5.7
414
+ @inline ( __always)
415
+ internal var _encodingBits : UInt64 {
416
+ _rawBits & ( Self . __utf8Bit | Self . __utf16Bit)
417
+ }
418
+
388
419
/// Returns true if the position in this index can be interpreted as an offset
389
420
/// into UTF-8-encoded string storage.
390
421
///
@@ -394,7 +425,7 @@ extension String.Index {
394
425
@inline ( __always)
395
426
internal var _canBeUTF8 : Bool {
396
427
// 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
398
429
}
399
430
400
431
/// Returns true if the position in this index can be interpreted as offset
@@ -407,45 +438,30 @@ extension String.Index {
407
438
@inline ( __always)
408
439
internal var _canBeUTF16 : Bool {
409
440
// 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
411
442
}
412
443
413
444
/// Returns the same index with the UTF-8 bit set.
414
445
@_alwaysEmitIntoClient // Swift 5.7
415
446
@inline ( __always)
416
- internal var _knownUTF8 : Self { Self ( _rawBits | 0x4 ) }
447
+ internal var _knownUTF8 : Self { Self ( _rawBits | Self . __utf8Bit ) }
417
448
418
449
/// Returns the same index with the UTF-16 bit set.
419
450
@_alwaysEmitIntoClient // Swift 5.7
420
451
@inline ( __always)
421
- internal var _knownUTF16 : Self { Self ( _rawBits | 0x8 ) }
452
+ internal var _knownUTF16 : Self { Self ( _rawBits | Self . __utf16Bit ) }
422
453
423
454
/// Returns the same index with both UTF-8 & UTF-16 bits set.
424
455
@_alwaysEmitIntoClient // Swift 5.7
425
456
@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
+ }
445
460
446
461
@_alwaysEmitIntoClient // Swift 5.7
447
462
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) )
449
465
}
450
466
}
451
467
@@ -487,7 +503,7 @@ extension String.Index {
487
503
}
488
504
489
505
d += " , encoding: "
490
- switch ( __isUTF8 , __isUTF16 ) {
506
+ switch ( _rawBits & Self . __utf8Bit != 0 , _rawBits & Self . __utf16Bit != 0 ) {
491
507
case ( false , false ) : d += " unknown "
492
508
case ( true , false ) : d += " utf8 "
493
509
case ( false , true ) : d += " utf16 "
0 commit comments