感受一下字符串相关的源文件个数

String 概览

  • 是一个结构体
  • 只有一个变量,类型是 _StringGuts


如上所示,String 真正的内容在__StringStorage或者__SharedStringStorage里面。

  private static func create(
realCodeUnitCapacity: Int, countAndFlags: CountAndFlags
) -> __StringStorage {
let storage = Builtin.allocWithTailElems_2(
__StringStorage.self,
realCodeUnitCapacity._builtinWordValue, UInt8.self,
1._builtinWordValue, Optional<_StringBreadcrumbs>.self)
#if arch(i386) || arch(arm)
storage._realCapacity = realCodeUnitCapacity
storage._count = countAndFlags.count
storage._flags = countAndFlags.flags
#else
storage._realCapacityAndFlags =
UInt64(truncatingIfNeeded: realCodeUnitCapacity)
storage._countAndFlags = countAndFlags
#endif storage._breadcrumbsAddress.initialize(to: nil)
storage.terminator.pointee = 0 // nul-terminated // NOTE: We can't _invariantCheck() now, because code units have not been
// initialized. But, _StringGuts's initializer will.
return storage
}

这里是真正分配内存的地方。

标记位

String 里有若干标记位,表示不同类型,一共有4位,被称为 discriminator

On 64-bit platforms, the discriminator is the most significant 4 bits of the bridge object.

字符串粗略可以分为Small stringsLarge strings

几乎所有的字符串操作,都根据是否是Small string来做了区分,比如判断是否是ACSCII

  //
// Whether the string is all ASCII
//
@inlinable
internal var isASCII: Bool {
@inline(__always) get {
if isSmall { return smallIsASCII }
return _countAndFlags.isASCII
}
}

_StringObject里获取并判断标记位

获取标记位

  internal var discriminatedObjectRawBits: UInt64 {
return Builtin.reinterpretCast(_object)
}

即bridge object的最高位。

判断是否可变

  @inlinable
internal var isImmortal: Bool {
@inline(__always) get {
return (discriminatedObjectRawBits & 0x8000_0000_0000_0000) != 0
}
}

判断是否是 small string

  internal var isSmall: Bool {
@inline(__always) get {
return (discriminatedObjectRawBits & 0x2000_0000_0000_0000) != 0
}
}

判断是否提供了连续的UTF8 code point

  // Whether this string can provide access to contiguous UTF-8 code units:
// - Small strings can by spilling to the stack
// - Large native strings can through an offset
// - Shared strings can:
// - Cocoa strings which respond to e.g. CFStringGetCStringPtr()
// - Non-Cocoa shared strings
@inlinable
internal var providesFastUTF8: Bool {
@inline(__always) get {
return (discriminatedObjectRawBits & 0x1000_0000_0000_0000) == 0
}
}

Swift 里字符串(一)概览的更多相关文章

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

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

  2. Swift 里字符串(七)stringIndex

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

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

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

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

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

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

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

  6. Swift 里字符串(九)UTF16View

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

  7. Swift 里字符串(八)UnicodeScalarView

    即以 Unicode Scarlar 的方式来查看字符串. /// let flag = "

  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. hibernate4 , spring3 使用 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 报错 Implementing class

    错误代码如下 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with ...

  2. source Insight工程的简单使用

    本文以管理虚拟机里面的uboot为例: 1.选择project->New project->选择工程路径,假设为D:\uboot:->project has its own conf ...

  3. c++中类的静态成员对象

    在c++中,可以声明一个静态的成员对象,但是此时仅仅声明,没有定义,也不会创建这个内部的静态成员对象.只有在类体外部定以后才能创建这个对象. #include<iostream> usin ...

  4. yii2缓存

    use yii\caching\Cache;$cache = Yii::$app->cache;$cache['var1'] = $value1;  // equivalent to: $cac ...

  5. PHP中的mb_convert_encoding与iconv函数介绍

    php传输乱码 mb_convert_encoding这个函数是用来转换编码的.原来一直对程序编码这一概念不理解,不过现在好像有点开窍了. 不过英文一般不会存在编码问题,只有中文数据才会有这个问题.比 ...

  6. nexus 参考文档

    参考文档: http://books.sonatype.com/nexus-book/reference/index.html E:\e\nexus\nexus-2.12.0-01\conf\nexu ...

  7. Hibernate和spring中的session总结

    1.this.getSession() 是org.springframework.orm.hibernate3.support.HibernateDaoSupport 中的一个方法,它可以从当前事务或 ...

  8. lua的table的删除操作

    直接使用table的remove方法 , , } table.remove(tab) --默认删除最后一个元素,第二个参数可以指定删除位置 删除后,后面的元素会往前移动.有点像C++的std::vec ...

  9. cxgrid动态显示行号

    uses cxLookAndFeelPainters; type TMyCxGrid = class(TObject)    class procedure DrawIndicatorCell(    ...

  10. sun.jersey使用Jackson转换数据

    差点被com.sun.jersey自身的json转换吓死,遇到List等类型,会把这些也转换为json对象,而不是jsonarray. 被园里的同行拯救了,在web.xml中配置一下就ok. < ...