Swift 里字符串(九)UTF16View
即以 UTF16
编码的格式来查看字符串。
UTF16View
是一个结构体
@_fixed_layout
public struct UTF16View {
@usableFromInline
internal var _guts: _StringGuts
@inlinable
internal init(_ guts: _StringGuts) {
self._guts = guts
_invariantCheck()
}
}
UTF16View
遵守 BidirectionalCollection
协议
可以正向/反向遍历,核心代码如下:
@inlinable @inline(__always)
public func index(after i: Index) -> Index {
if _slowPath(_guts.isForeign) { return _foreignIndex(after: i) }
if _guts.isASCII { return i.nextEncoded }
// For a BMP scalar (1-3 UTF-8 code units), advance past it. For a non-BMP
// scalar, use a transcoded offset first.
let len = _guts.fastUTF8ScalarLength(startingAt: i.encodedOffset)
if len == 4 && i.transcodedOffset == 0 {
return i.nextTranscoded
}
return i.strippingTranscoding.encoded(offsetBy: len)
}
读写String
的 utf16
属性
public var utf16: UTF16View {
@inline(__always) get { return UTF16View(_guts) }
@inline(__always) set { self = String(newValue._guts) }
}
每次读,都会生成一个新的UTF16View
。
每次写,都会更新String
内部的_guts
。
UTF8View
属性
和UTF16View
类似,也是一个结构体,也遵守BidirectionalCollection
协议。
public var utf8: UTF8View {
@inline(__always) get { return UTF8View(self._guts) }
set { self = String(newValue._guts) }
}
Swift 里字符串(九)UTF16View的更多相关文章
- 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 里字符串(七)stringIndex
在 String 里,用来索引 Character 的,不是整数,而是StringIndex 内部结构 extension String { /// A position of a character ...
- Swift里字符串(六)Shared strings
Shared strings do not have tail-allocated storage, but can provide access upon query to contiguous U ...
- Swift 里字符串(三)small String
 small string, 只有两个 UInt64 的字,这里面存储了所有的信息. 内存布局如下:  第二个 UInt64 存储了标记位和长度信息,以及部分字符串的值 // Get an int ...
- Swift 里字符串(四)large sting
对于普通的字符串,对应的_StringObject 有两个存储属性: _countAndFlagsBits: UInt64 _object: Builtin.BridgeObject _countAn ...
- Swift 里字符串(一)概览
感受一下字符串相关的源文件个数  String 概览 是一个结构体 只有一个变量,类型是 _StringGuts  如上所示,String 真正的内容在__StringStorage或者__Sha ...
- Swift 里字符串(八)UnicodeScalarView
即以 Unicode Scarlar 的方式来查看字符串. /// let flag = "
- Swift 里字符串(十一)OC 字符串和 Swift 字符串的转换
 to OC func _bridgeToObjectiveCImpl() -> AnyObject { if _guts.isSmall { return _guts.asSmall.wit ...
随机推荐
- Cordova学习
Cordova学习 ui线程里处理耗时逻辑 runOnUiThread(new Runnable() { public void run() { //处理 } });
- springmvc cfx 整合
1.先看一下目录列表,本人用的是eclipse 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xm ...
- 31. The New Bread Earners 挣钱养家的新军
31. The New Bread Earners 挣钱养家的新军 ① They call them the new bread earners.They are women,and they are ...
- 字典树Java实现
Trie树的原理 Trie树也称字典树,因为其效率很高,所以在在字符串查找.前缀匹配等中应用很广泛,其高效率是以空间为代价的. 利用串构建一个字典树,这个字典树保存了串的公共前缀信息,因此可以降低查询 ...
- Linux 修改 IP地址 和 网关
修改IP地址和网关是很常见的操作,在做相关实验的时候,如果没有设置好,会带来很多不必要的麻烦.. 1. 修改IP地址vi /etc/sysconfig/network-scripts/ifcfg-et ...
- 20170908工作日记--Volley源码详解
Volley没有jar包,需要从官网上下载源码自己编译出来,或者做成相关moudle引入项目中.我们先从最简单的使用方法入手进行分析: //创建一个网络请求队列 RequestQueue reques ...
- 20155320 2016-2017-2 《Java程序设计》第五周学习总结
20155320 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 错误处理 java中所有错误都会被打包为对象,可以通过try catch 代表错误的对象后做 ...
- Netty Nio启动全流程
Netty Nio启动全流程 1. 各组件之间的关系 说明:EventLoopGroup类似线程池,EventLoop为单线程,每个EventLoop关联一个Nio Selector,用于注册Chan ...
- spring mvc学习笔记(一)web.xml文件配置的一点重要信息
通过这个web.xml文件可以看出,所有的*.shtml的请求,都会被springmvc这个servlet处理.这里如果没有指定contextConfigLocation这个参数,将会按照默认规则在c ...
- Ajax请求php返回json对象数据中包含有数字索引和字符串索引,在for in循环中取出数据的顺序问题
//php中的数组数据格式 Array ( [all] => Array ( [title] => ALL [room_promotion_id] => all ) [best_av ...