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。然后把discriminatortrailing结合,作为第二个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的更多相关文章

  1. Swift 里字符串(七)stringIndex

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

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

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

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

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

  4. Swift 里字符串(四)large sting

    对于普通的字符串,对应的_StringObject 有两个存储属性: _countAndFlagsBits: UInt64 _object: Builtin.BridgeObject _countAn ...

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

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

  6. [Swift]LeetCode394. 字符串解码 | Decode String

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  7. Swift 里字符串(九)UTF16View

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

  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. mvc模拟实现

    .定义httpmodule <system.webServer> <modules> <add name="UrlRoutingModule" typ ...

  2. 写点C++ 学习记录 充数

    #include "stdafx.h" #include <cstdlib> #include <iostream> using namespace std ...

  3. jquery panel加载(dialog加载类似)

    项目情况: 主页面用引用了公共头文件(包含easui.min.js),使用easyui的dialog(href方式)打开窗口(被打开的窗口页面是纯html片段,无body元素等,也引入了公共头文件), ...

  4. 【Web】网站主如何更改网页标签的图标(favicon.ico)

    修改web项目的favicon图标,方式有两种:全局方式和局部方式 全局方式: 进入服务器\webapps\ROOT,然后用自己的favicon.ico替换服务器自带的favicon.ico图片 局部 ...

  5. static 成员函数

    和静态数据成员一样,静态成员函数是所有对象共享的,不是单独属于某一个对象,由于静态成员函数没有传递this指针,故static member function 只能访问static成员,不能访问非st ...

  6. hdu1302 The Snail

    题目 题目大意: 一只蜗牛在H英尺高的底部,想爬到顶端.蜗牛可以在太阳升起的时候爬上U英尺,但是在晚上睡觉的时候会滑下D英尺.蜗牛的疲劳系数为F(百分比),                       ...

  7. hibernate 一对多,由谁维护性能最优

    举例如下 Customer类: public class Customer { private int id; private String name; private Set orders = ne ...

  8. delphi 手机振动 IOS Android

    delphi 手机振动 IOS Android delphi  手机振动 IOS Android 振动 https://community.embarcadero.com/blogs/entry/ho ...

  9. Android-AppUtils工具类

    常用APP的工具类,包含版本号.版本名称.安装的应用程序ICON public class AppUtils { private AppUtils(){} /** * 设置状态栏的颜色 * @para ...

  10. List<T>用法

    所属命名空间:System.Collections.Generic public class List<T> : IList<T>, ICollection<T>, ...