对于普通的字符串,对应的_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)
}

最新文章

  1. 【2016.3.30项目技术记录】]VS2010自动生成MFC单文档框架程序的修改:去除属性框,在CViewTree类中添加鼠标单击响应
  2. 基于.NET C#的 sqlite 数据库 ORM 【Easyliter】
  3. Git版本控制工具(三)----远程仓库GitHub的使用
  4. 4、WPF应用程序的启动
  5. 初识SQL注入
  6. c#xml序列化对象,xml标记都缩写了
  7. 获取自动增长IDmysqli函数
  8. Servlet配置load-on-startup
  9. Orchard是一个了不起CMS(内容管理系统)
  10. 发放福利:原价135元/年的阿里云CDN流量包(500GB)免费送
  11. ThinkPhp_5框架开发【整理】
  12. 使用domain模块捕获异步回调中的异常
  13. Maven+SSM框架搭建【spring+springmvc+mybatis】
  14. C# 对MongoDB 进行增删改查的简单操作
  15. C\C++控制台程序隐藏方法总结
  16. February 23rd, 2018 Week 8th Friday
  17. javascript 练习题目答案1
  18. 【做题】HDU6331 Walking Plan——矩阵&amp;分块
  19. HTML 学习杂记
  20. node的 node-sass@^4.11.0 出现:npm: no such file or directory, scandir &#39;.../node_modules/node-sass/vendor&#39;

热门文章

  1. Maven系列(一)plugin
  2. Jquery Mobile 随记
  3. MetroApp保存UIEment为图片
  4. 2018.08.29 NOIP模拟 table(拓扑排序+建图优化)
  5. 43 We were Born to Nap 我们天生需要午睡
  6. java常用设计模式一:单例模式
  7. gulp布局构建小结
  8. C++中的关键字用法--- explicit
  9. n&&m and n||m 的区别
  10. Berlin 10.1 支持 iPhone 4 (iOS v7.x)