对于普通的字符串,对应的_StringObject 有两个存储属性:

  1. _countAndFlagsBits: UInt64
  2. _object: Builtin.BridgeObject

_countAndFlagsBits

存储者字符串的长度和一些标记位。

┌─────────┬───────┬──────────────────┬─────────────────┬────────┬───────┐
│ b63 │ b62 │ b61 │ b60 │ b59:48 │ b47:0 │
├─────────┼───────┼──────────────────┼─────────────────┼────────┼───────┤
│ isASCII │ isNFC │ isNativelyStored │ isTailAllocated │ TBD │ count │
└─────────┴───────┴──────────────────┴─────────────────┴────────┴───────┘

其中高16位是flag,低48位为字符串的长度,是utf8 code point的长度,而不是人眼看到的字符的个数。

  @inlinable @inline(__always)
internal init(count: Int, flags: UInt16) {
// Currently, we only use top 4 flags
_internalInvariant(flags & 0xF000 == flags) let rawBits = UInt64(truncatingIfNeeded: flags) &<< 48
| UInt64(truncatingIfNeeded: count)
self.init(raw: rawBits)
_internalInvariant(self.count == count && self.flags == flags)
}

_object

真正字符串的位置。高四位是 discriminator,指示着字符串的一些属性。

On 64-bit platforms, the discriminator is the most significant 4 bits of the bridge object.

字符串的分类

Large strings can either be "native", "shared", or "foreign".

Native strings have tail-allocated storage, which begins at an offset of
nativeBias from the storage object's address. String literals, which reside
in the constant section, are encoded as their start address minus nativeBias,
unifying code paths for both literals ("immortal native") and native strings.
Native Strings are always managed by the Swift runtime.

Shared strings do not have tail-allocated storage, but can provide access
upon query to contiguous UTF-8 code units. Lazily-bridged NSStrings capable of
providing access to contiguous ASCII/UTF-8 set the ObjC bit. Accessing shared
string's pointer should always be behind a resilience barrier, permitting
future evolution.

Foreign strings cannot provide access to contiguous UTF-8. Currently, this only
encompasses lazily-bridged NSStrings that cannot be treated as "shared". Such
strings may provide access to contiguous UTF-16, or may be discontiguous in
storage. Accessing foreign strings should remain behind a resilience barrier
for future evolution. Other foreign forms are reserved for the future.

native shared foreign
tail-allocated
连续UTF-8 code unit

NSString 的转换

  // Whether the object stored can be bridged directly as a NSString
@usableFromInline // @opaque
internal var hasObjCBridgeableObject: Bool {
@_effects(releasenone) get {
// Currently, all mortal objects can zero-cost bridge
return !self.isImmortal
}
} // Fetch the stored subclass of NSString for bridging
@inline(__always)
internal var objCBridgeableObject: AnyObject {
_internalInvariant(hasObjCBridgeableObject)
return Builtin.reinterpretCast(largeAddressBits)
}

Swift 里字符串(四)large sting的更多相关文章

  1. Swift 里字符串(十)修改字符串

    以append操作为例 public mutating func append(_ other: String) { if self.isEmpty && !_guts.hasNati ...

  2. Swift 里字符串(一)概览

    感受一下字符串相关的源文件个数  String 概览 是一个结构体 只有一个变量,类型是 _StringGuts  如上所示,String 真正的内容在__StringStorage或者__Sha ...

  3. Swift 里字符串(七)stringIndex

    在 String 里,用来索引 Character 的,不是整数,而是StringIndex 内部结构 extension String { /// A position of a character ...

  4. Swift里字符串(五)Native strings

    Native strings have tail-allocated storage, which begins at an offset of nativeBias from the storage ...

  5. Swift 里字符串(三)small String

     small string, 只有两个 UInt64 的字,这里面存储了所有的信息. 内存布局如下:  第二个 UInt64 存储了标记位和长度信息,以及部分字符串的值 // Get an int ...

  6. Swift 里字符串(九)UTF16View

    即以 UTF16 编码的格式来查看字符串. UTF16View 是一个结构体 @_fixed_layout public struct UTF16View { @usableFromInline in ...

  7. Swift 里字符串(八)UnicodeScalarView

    即以 Unicode Scarlar 的方式来查看字符串. /// let flag = "

  8. Swift里字符串(六)Shared strings

    Shared strings do not have tail-allocated storage, but can provide access upon query to contiguous U ...

  9. Swift 里字符串(十一)OC 字符串和 Swift 字符串的转换

     to OC func _bridgeToObjectiveCImpl() -> AnyObject { if _guts.isSmall { return _guts.asSmall.wit ...

随机推荐

  1. action spring 注入错误,如果检查各项注入都没有错误时,考虑struts 是否配置了namespace(如果你有多个namespace="/")

    [ERROR] 2015-01-04 09:42:35,180 (CommonsLogger.java:38) - Exception occurred during processing reque ...

  2. 2018.10.20 bzoj1925: [Sdoi2010]地精部落(dp)

    传送门 dp好题. 设f[i][j]f[i][j]f[i][j]表示iii个数结尾是jjj且结尾两个数递增的方案数. 那么显然可以对称的定义出g[i][j]g[i][j]g[i][j]表示iii个数结 ...

  3. 处理jQuery选择器中的特殊符号,如(、#等

    前几天解决一个外网问题,客服反馈页面数据加载不出来,首先看一下服务端日志也没报错异常,自己测试了一下,在chrome的Console发现有js报错,原来是js报错导致的数据加载不出来. 调试了一番,发 ...

  4. Java 窗体居中 通用代码

    Toolkit kit = Toolkit.getDefaultToolkit();    // 定义工具包    Dimension screenSize = kit.getScreenSize() ...

  5. webuploader 文件上传插件 IE8/9 文件选择不上传

    IE8/9下文件上传是采用flash模式,一直发送http://xxx.xxx.xx.xx:8888/crossdomain.xml请求,状态码为404,原因是上传文件的服务器未配置crossdoma ...

  6. 多线程中使用curl致coredump问题

    coredump时的调用栈: #0  0x081eff2c in addbyter () #1  0x081f05b8 in dprintf_formatf () #2  0x081f15cf in ...

  7. 201709021工作日记--CAS解读

    CAS主要参考博文:classtag  http://www.jianshu.com/p/473e14d5ab2d CAS(Compare and swap)比较和替换是设计并发算法时用到的一种技术 ...

  8. SoC FPGA JTAG电路设计 要点

    JTAG协议制定了一种边界扫描的规范,边界扫描架构提供了有效的测试布局紧凑的PCB板上元件的能力.边界扫描可以在不使用物理测试探针的情况下测试引脚连接,并在器件正常工作的过程中捕获运行数据. SoC ...

  9. [诈骗]“中国移动”发送诈骗短信,china mobile 是骗子吗?

    今年,这是我遇到的第二次短信诈骗了,两次的手法都是完全一样的.第一次,冒充的是招商银行的积分活动,结果我还真的输入了银行卡与取款密码. 输入完之后,我才醒悟,然后立刻打招商客服电话咨询,改行是否在进行 ...

  10. Beyond Compare脚本:命令行批量比较文件并生成html格式的差异报告

    BComp.exe /silent /closescript /solo @E:\compareTest\BCbatch.txt text-report layout:side-by-side opt ...