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 ...
随机推荐
- Directory /usr/local/hadoop/tmp/tmp/hadoop-root/dfs/name is in an inconsistent state: storage directory does not exist or is not accessible
解决方法: <property> <name>hadoop.tmp.dir</name> <value>/usr/local/hadoop/tmp< ...
- 关闭文件流--fclose,
头文件:#include<stdio.h> 函数原型:int fclose(FILE *fp) 参数说明:fp将被关闭的文件指针 返回值:成功返回0,失败返回EOF宏.
- 分组取前N记录
分组取前N记录 经常看到问题,如何取出每组的前N条记录.方便大家参考于是便把常见的几种解法列出于下. 问题:有表 如下,要求取出各班前两名(允许并列第二)Table1+----+------+-- ...
- 2018.10.23 bzoj1297: [SCOI2009]迷路(矩阵快速幂优化dp)
传送门 矩阵快速幂优化dp简单题. 考虑状态转移方程: f[time][u]=∑f[time−1][v]f[time][u]=\sum f[time-1][v]f[time][u]=∑f[time−1 ...
- Cygwin工具的简单使用
简介 从使用角度来看:Cygwin就是一个windows软件,该软件就是在windows上仿真linux操作系统.简言之,cygwin是一个在windows平台上运行的 linux模拟环境,使用一个D ...
- html转jsp部分css不可用
解决方法 <%String path = request.getContextPath();String basePath = request.getScheme()+"://&quo ...
- java-Runtime 调用命令
java是一个跨平台的语言,可以在多种平台上运行相应的程序,但是有些时候进行数据的提取时,可能就需要系统的相关命令,尤其是调用linux命令 这时候就需要使用java的Runtime命令,来执行lin ...
- php一些方法说明
var_dump():判断一个变量的类型与长度,并输出变量的数值,如果变量有值输的是变量的值并回返数据类型.此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值.数组将递归展开值,通过缩进显 ...
- struts2和JSON的数据交互
一.实验环境 1.struts2基本包 2.json-plugin 在struts2的lib下可以找到. 3.web.xml 加入struts2 <filter> <filter-n ...
- 20155209 2016-2017-2 《Java程序设计》第八周学习总结
20155209 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 Java NIO(New IO)是一个可以替代标准Java IO API的IO API(从J ...