Swift 里字符串(四)large sting
对于普通的字符串,对应的_StringObject 有两个存储属性:
_countAndFlagsBits: UInt64_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
nativeBiasfrom the storage object's address. String literals, which reside
in the constant section, are encoded as their start address minusnativeBias,
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的更多相关文章
- Swift 里字符串(十)修改字符串
以append操作为例 public mutating func append(_ other: String) { if self.isEmpty && !_guts.hasNati ...
- Swift 里字符串(一)概览
感受一下字符串相关的源文件个数  String 概览 是一个结构体 只有一个变量,类型是 _StringGuts  如上所示,String 真正的内容在__StringStorage或者__Sha ...
- Swift 里字符串(七)stringIndex
在 String 里,用来索引 Character 的,不是整数,而是StringIndex 内部结构 extension String { /// A position of a character ...
- Swift里字符串(五)Native strings
Native strings have tail-allocated storage, which begins at an offset of nativeBias from the storage ...
- Swift 里字符串(三)small String
 small string, 只有两个 UInt64 的字,这里面存储了所有的信息. 内存布局如下:  第二个 UInt64 存储了标记位和长度信息,以及部分字符串的值 // Get an int ...
- Swift 里字符串(九)UTF16View
即以 UTF16 编码的格式来查看字符串. UTF16View 是一个结构体 @_fixed_layout public struct UTF16View { @usableFromInline in ...
- Swift 里字符串(八)UnicodeScalarView
即以 Unicode Scarlar 的方式来查看字符串. /// let flag = "
- Swift里字符串(六)Shared strings
Shared strings do not have tail-allocated storage, but can provide access upon query to contiguous U ...
- Swift 里字符串(十一)OC 字符串和 Swift 字符串的转换
 to OC func _bridgeToObjectiveCImpl() -> AnyObject { if _guts.isSmall { return _guts.asSmall.wit ...
随机推荐
- 如何使用tapd?
tapd 可以编写测试用例 测试计划等 敏捷开发常用的工具.稍后会更新..
- 关闭文件流--fclose,
头文件:#include<stdio.h> 函数原型:int fclose(FILE *fp) 参数说明:fp将被关闭的文件指针 返回值:成功返回0,失败返回EOF宏.
- 2018上IEC计算机高级语言(C)作业 第0次作业
最理想的师生关系是健身教练和学员的关系,在这种师生关系中你期望获得来自老师的哪些帮助? 最理想的的师生关系是健身教练和学员的关系,其实我个人感觉不太认同,我觉得老师和学生之间更多的是一种共生关系,像植 ...
- 2018.09.27 bzoj3029: 守卫者的挑战(概率dp)
传送门 概率dp经典题目. 直接f[i][j][k]f[i][j][k]f[i][j][k]表示当前是第i次挑战,已经胜利了j次,目前的背包剩余空间是k. 然后用前面的转移后面的就行了. 注意第三维可 ...
- 2018.09.24 codeforces 1051F. The Shortest Statement(dijkstra+lca)
传送门 这真是一道一言难尽的题. 首先比赛的时候居然没想出来正解. 其次赛后调试一直调不出来最后发现是depth传错了. 其实这是一道简单题啊. 对于树边直接lca求距离. 由于非树边最多21条. 因 ...
- 2018.09.19 atcoder Snuke's Subway Trip(最短路)
传送门 就是一个另类最短路啊. 利用颜色判断当前节点的最小花费的前驱边中有没有跟当前的边颜色相同的. 如果有这条边费用为0,否则费用为1. 这样跑出来就能ac了. 代码: #include<bi ...
- 2018.08.06 bzoj1500: [NOI2005]维修数列(非旋treap)
传送门 平衡树好题. 我仍然是用的fhqtreap,感觉速度还行. 维护也比线段树splay什么的写起来简单. %%%非旋treap大法好. 代码: #include<bits/stdc++.h ...
- 继承方法-->call继承
function Person(name,age,sex){ this.name = name; this.age = age; this.sex = sex; }function P1(name,a ...
- 文件读取ndarry 等价于DataFrame的操作
LD=loadDatas() userMat=LD.makeRatingMatWithoutUserID() print(type(userMat)) userRatingMat=pd.DataFra ...
- python 修改文件编码方式
import chardet import os def strJudgeCode(str): return chardet.detect(str) def readFile(path): try: ...