Swift 里字符串(三)small String

small string, 只有两个 UInt64 的字,这里面存储了所有的信息。
内存布局如下:

第二个 UInt64
存储了标记位和长度信息,以及部分字符串的值
// Get an integer equivalent to the _StringObject.discriminatedObjectRawBits
// computed property.
@inlinable @inline(__always)
internal var rawDiscriminatedObject: UInt64 {
// Reverse the bytes on big-endian systems.
return _storage.1.littleEndian
}
| rawbit.1 | 值 | 含义 |
|---|---|---|
| b63 | 1 | 是不可变的 |
| b62 | 0/1 | 是否是ASCII |
| b61 | 1 | 是 small string |
| b60 | 0 | 可以获取联系utf8 code point |
| b59-b56 | 0000~1111 | 已经使用的长度 |
| b55~b0 | 存储utf8 code point |
第一个UInt64
存储的都是字符串的值
初始化 small string
最基本的初始化
@inlinable @inline(__always)
internal init(leading: UInt64, trailing: UInt64, count: Int) {
_internalInvariant(count <= _SmallString.capacity)
let isASCII = (leading | trailing) & 0x8080_8080_8080_8080 == 0
let discriminator = _StringObject.Nibbles
.small(withCount: count, isASCII: isASCII)
.littleEndian // reversed byte order on big-endian platforms
_internalInvariant(trailing & discriminator == 0)
self.init(raw: (leading, trailing | discriminator))
_internalInvariant(self.count == count)
}
这是最基本的初始化方法。先根据是否是ASCII和长度值,生成一个discriminator。然后把discriminator和trailing结合,作为第二个UInt64。
根据缓存区初始化
// Direct from UTF-8
@inlinable @inline(__always)
internal init?(_ input: UnsafeBufferPointer<UInt8>) {
if input.isEmpty {
self.init()
return
}
let count = input.count
guard count <= _SmallString.capacity else { return nil }
// TODO(SIMD): The below can be replaced with just be a masked unaligned
// vector load
let ptr = input.baseAddress._unsafelyUnwrappedUnchecked
let leading = _bytesToUInt64(ptr, Swift.min(input.count, 8))
let trailing = count > 8 ? _bytesToUInt64(ptr + 8, count &- 8) : 0
self.init(leading: leading, trailing: trailing, count: count)
}
即先判断长度是否超过上限,如果超过,返回nil。
如果没有超过上限,再调用init(leading: UInt64, trailing: UInt64, count: Int) 方法。
small string 创建过程

Swift 里字符串(三)small String的更多相关文章
- Swift 里字符串(七)stringIndex
在 String 里,用来索引 Character 的,不是整数,而是StringIndex 内部结构 extension String { /// A position of a character ...
- Swift 里字符串(十)修改字符串
以append操作为例 public mutating func append(_ other: String) { if self.isEmpty && !_guts.hasNati ...
- Swift里字符串(五)Native strings
Native strings have tail-allocated storage, which begins at an offset of nativeBias from the storage ...
- Swift 里字符串(四)large sting
对于普通的字符串,对应的_StringObject 有两个存储属性: _countAndFlagsBits: UInt64 _object: Builtin.BridgeObject _countAn ...
- Swift 里字符串(一)概览
感受一下字符串相关的源文件个数  String 概览 是一个结构体 只有一个变量,类型是 _StringGuts  如上所示,String 真正的内容在__StringStorage或者__Sha ...
- [Swift]LeetCode394. 字符串解码 | Decode String
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- Swift 里字符串(九)UTF16View
即以 UTF16 编码的格式来查看字符串. UTF16View 是一个结构体 @_fixed_layout public struct UTF16View { @usableFromInline in ...
- 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 ...
随机推荐
- easyui datagrid编辑
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Datagrid.aspx. ...
- 关闭文件描述符-close
头文件:#include<unistd.h> 原型:int close(int fd); 返回值:成功返回0,失败返回-1.
- part1:12-sudo用户管理和Linux密码故障排除
sudo用户管理 权力下放! 1.Root密码破解步骤 a.在系统启动时,迅速按下任意键或回车键.进入grub选项菜单 b.在grub选项菜单按e进入编辑模式,上下选择kernel行.然后按e进入编辑 ...
- cucumber安装可能发生的错误
1.--ignore-certification-errors 解决:可能是你的chromedriver版本与ruby版本不匹配,换一个版本 2.找不到文件,certification verify ...
- python之基础补充
一 bit,和bytes的关系 bit:就是计算机的最小的表示单位. bytes:就是计算机的最小的储存单位. 1 字节(bytes) = 8 位(bit) 格式: print(bytes('字符' ...
- 2018.08.10 atcoder No Need(线性dp)
传送门 输入一个序列an" role="presentation" style="position: relative;">anan,输入k&q ...
- mysql 查询表 的所有字段名称
select COLUMN_NAME from information_schema.COLUMNS where table_name = 'your_table_name' and table_sc ...
- 44 The shopping psychology 购物心理
The shopping psychology 购物心理 ①People can be addicted to different things ---e. g.,alcohol, drugs, ce ...
- flask_web表单
一.配置 1.为了能够处理 web 表单,我们将使用 Flask-WTF,该扩展封装了 WTFForms 并且恰当地集成进 Flask 中.许多 Flask 扩展需要大量的配置,因此我们将要在 mic ...
- springMVC学习(注解实现依赖注入)
原文:http://blog.csdn.net/mockingbirds/article/details/45399691 上一篇博客,学习了spring的依赖注入,即利用spring容器来为类中的属 ...