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 ...
随机推荐
- db2学习笔记
a.服务端安装 v11.1_win64_expc.zip 官网下载 b.客户端安装 Toad for DB2 Freeware 6.1 百度找找 .建数据库 create database HRA_G ...
- 设计模式之java源码-工厂方法模式
工厂方法模式 8.1 女娲造人的故事 东汉<风俗通>记录了一则神话故事:“开天辟辟,未有人民,女娲搏,黄土作人……”,讲述的内容就是大家非常熟悉的女娲造人的故事.开天辟地之初,大地上并没有 ...
- c++11多线程学习笔记之二 mutex使用
// 1111111.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include ...
- Number.isInteger在IE中报错的解决方法
if (!Number.isInteger) { Number.isInteger = function(num) { return typeof num == "number" ...
- 2018.10.16 NOIP模拟 膜法(组合数学)
传送门 原题,原题,全TM原题. 不得不说天天考原题. 其实这题我上个月做过类似的啊,加上dzyodzyodzyo之前有讲过考试直接切了. 要求的其实就是∑i=lr(ii−l+k)\sum _{i=l ...
- 有趣的NaN类型
在学习Java集合的时候遇到了Float.isNaN(float)函数,点进去一看就不理解了,函数实现如下: public static boolean isNaN(float v) { return ...
- 在 web 容器中运行 cxf
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC &q ...
- 201709021工作日记--Volley源码详解(五)
学习完了CacheDispatcher这个类,下面我们看下NetworkDispatcher这个类的具体细节,先上代码: /** * 提供一个线程执行网络调度的请求分发 * Provides a th ...
- hdu 5009 离散化
http://acm.hdu.edu.cn/showproblem.php?pid=5009 有一段序列,涂连续一段子序列的代价为该子序列出现不同数字个数的平方,求最小代价涂完整个序列. ai有10^ ...
- Americans are usually tolerant (Listen speak of Unit 2)
Americans are usually 1) tolerant of non-native speakers who have some 2) trouble understanding Engl ...