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.

即字符串数据从内部_object的起始位置加上一个偏移处起始。
对应的类是__StringStorage

内存布局如下

在 OC runtime下,是 NSString 的子类。

子类化NSString

根据文档,提供了几个方法

  @objc(length)
final internal var length: Int {
@_effects(readonly) @inline(__always) get {
return asString.utf16.count // UTF16View special-cases ASCII for us.
}
}
  @objc(characterAtIndex:)
@_effects(readonly)
final internal func character(at offset: Int) -> UInt16 {
let str = asString
return str.utf16[str._toUTF16Index(offset)]
} @objc(getCharacters:range:)
@_effects(releasenone)
final internal func getCharacters(
_ buffer: UnsafeMutablePointer<UInt16>, range aRange: _SwiftNSRange
) {
_getCharacters(buffer, aRange)
}

Swift里字符串(五)Native strings的更多相关文章

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

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

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

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

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

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

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

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

  5. [Swift]LeetCode43. 字符串相乘 | Multiply Strings

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1and  ...

  6. Swift 里字符串(七)stringIndex

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

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

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

  8. [Swift]LeetCode415. 字符串相加 | Add Strings

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  9. Swift 里字符串(九)UTF16View

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

随机推荐

  1. css页面组件

    页面组件 1 元素的尺寸/边框/背景 1.1 css尺寸相关属性 height 高度 min-height 最小高度 max-height 最大高度 width 宽度 min-width 最小宽度 m ...

  2. hdu-1060(数学问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1061 思路:结论:a=10^(N*lg(N) - [lg(N^N)]); 证明:如果一直a是结果,则a ...

  3. flask_hello world

    对于flask框架的学习全部借鉴于http://www.pythondoc.com/flask-mega-tutorial/index.html 在学习的过程中,我使用的是Pycharm IDE,Py ...

  4. 按str 存储和按 list 存储

    按str 存储1 w2 = open('./trsd_w.txt','a')#a代表追加 w代表重写 if matcher1: flag = 1 w2.write("\n") fo ...

  5. 四则运算 Java (于泽浩,袁浩越)

    GitHub 地址 一. 项目要求 题目 实现一个自动生成小学四则运算题目的命令行程序. 需求(全部完成) 使用 -n 参数控制生成题目的个数 Myapp.exe -n 10 使用 -r 参数控制题目 ...

  6. Eclipse技巧

    1 alt + / 提示 2 ctrl + shift + g 查找方法被谁调用 3 ctrl + t 查看某个类的继承关系 4 alt + 上/下 移动当前行上或者下 5 ctrl + / 行注释 ...

  7. Internal Server Error - http code 500

    Eror Example 1 :  

  8. 【转】如何使用BehaviorSDK

    原文地址:http://blogs.msdn.com/b/windows8devsupport/archive/2014/10/24/behaviorsdk.aspx 前言 在开发过程中,程序员一般通 ...

  9. LeetCode140:Word Break II

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  10. Python 数据结构与算法——冒泡排序

    #方法一:递归 def bubble(lst,i): if i==1: return lst for j in range(i-1): if lst[j] > lst[j+1]: lst[j], ...