Swift String

作者: 幸运者_Lucky | 来源:发表于2020-07-27 17:52 被阅读0次

    String

    1. Performance Optimizations
      Although strings in Swift have value semantics, strings use a copy-on-write strategy to store their data in a buffer. This buffer can then be shared by different copies of a string. A string’s data is only copied lazily, upon mutation, when more than one string instance is using the same buffer. Therefore, the first in any sequence of mutating operations may cost O(n) time and space.
      When a string’s contiguous storage fills up, a new buffer must be allocated and data must be moved to the new storage. String buffers use an exponential growth strategy that makes appending to a string a constant time operation when averaged over many append operations.
    /*
      On 64-bit platforms, the discriminator is the most significant 4 bits of the bridge object.
      ┌─────────────────────╥─────┬─────┬─────┬─────┐
      │ Form                ║ b63 │ b62 │ b61 │ b60 │
      ╞═════════════════════╬═════╪═════╪═════╪═════╡
      │ Immortal, Small     ║  1  │ASCII│  1  │  0  │
      ├─────────────────────╫─────┼─────┼─────┼─────┤
      │ Immortal, Large     ║  1  │  0  │  0  │  0  │
      ╞═════════════════════╬═════╪═════╪═════╪═════╡
      │ Native              ║  0  │  0  │  0  │  0  │
      ├─────────────────────╫─────┼─────┼─────┼─────┤
      │ Shared              ║  x  │  0  │  0  │  0  │
      ├─────────────────────╫─────┼─────┼─────┼─────┤
      │ Shared, Bridged     ║  0  │  1  │  0  │  0  │
      ╞═════════════════════╬═════╪═════╪═════╪═════╡
      │ Foreign             ║  x  │  0  │  0  │  1  │
      ├─────────────────────╫─────┼─────┼─────┼─────┤
      │ Foreign, Bridged    ║  0  │  1  │  0  │  1  │
      └─────────────────────╨─────┴─────┴─────┴─────┘
      b63: isImmortal: Should the Swift runtime skip ARC
        - Small strings are just values, always immortal
        - Large strings can sometimes be immortal, e.g. literals
      b62: (large) isBridged / (small) isASCII
        - For large strings, this means lazily-bridged NSString: perform ObjC ARC
        - Small strings repurpose this as a dedicated bit to remember ASCII-ness
      b61: isSmall: Dedicated bit to denote small strings
      b60: isForeign: aka isSlow, cannot provide access to contiguous UTF-8
      The canonical empty string is the zero-sized small string. It has a leading
      nibble of 1110, and all other bits are 0.
      A "dedicated" bit is used for the most frequent fast-path queries so that they
      can compile to a fused check-and-branch, even if that burns part of the
      encoding space.
      On 32-bit platforms, we store an explicit discriminator (as a UInt8) with the
      same encoding as above, placed in the high bits. E.g. `b62` above is in
      `_discriminator`'s `b6`.
    */
    

    关于字符串定义

    1. 使用 """ ... """ 进行定义字符串, 可以包含空格等内容, 但是像 \n 这些符号, 不会自动加上\ 进行转义.
    let str = """
    \"""\n\r
    """
    let json = ["asdf": str]
    print(str)
    print(json.description)
    
    // 打印内容
    """
    
    ["asdf": "\"\"\"\n\r"]
    
    1. 使用 #""" ... """# 进行定义字符串, 所有需要转义的字符都会默认加上 \, 两者区别主要在此.
    let str1 = #"""
    \"""\n\r
    """#
    let json1 = ["asdf": str1]
    print(str1)
    print(json1.description)
    
    // 打印内容
    \"""\n\r
    ["asdf": "\\\"\"\"\\n\\r"]
    
    1. 关于起始位置, 从结尾的符号 """ 或者 """# 起始位置算起, 里面字符串和这个符号距离相差多远就会补多少空格在里面, 而且所有的文字内容的起始位置必须和结尾符号对齐, 或者在结尾符号之后, 否则编译报错.
    let str = """
         \"""\n\r
    """
    let json = ["asdf": str]
    print(str)
    print(json.description)
    
    // 打印内容
         """
    
    ["asdf": "     \"\"\"\n\r"]
    
    
    1. 关于 #" ... "# 使用, 关于 \n 这些字符, 会默认加上 \, 进行转义, 和 " ... " 相比较和上面是相同的, " ... " 就不具体说了.
    let str2 = #"asdfasd \#(1000).\n"#
    let json2 = ["asdf": str2]
    print(str2)
    print(json2.description)
    
    // 打印内容
    asdfasd 1000.\n
    ["asdf": "asdfasd 1000.\\n"]
    

    相关文章

      网友评论

        本文标题:Swift String

        本文链接:https://www.haomeiwen.com/subject/amqtrktx.html