Swift 2.1 [UInt8] --utf8--> String?

前端 未结 3 1778
小蘑菇
小蘑菇 2020-12-04 03:10

I know questions like this exist on both Stack Overflow and elsewhere. But it seems to have evolved a lot as well.

Given a list of UInt8 (a swift byte a

3条回答
  •  抹茶落季
    2020-12-04 03:41

    I actually ended up needing to do this for a stream of UInt8 and was curious how hard utf8 decoding is. It's definitely not a one liner, but through the following direct implementation together:

    import UIKit
    
    let bytes:[UInt8] = [0xE2, 0x82, 0xEC, 0x00]
    
    var g = bytes.generate()
    
    extension String {
        init(var utf8stream:IndexingGenerator<[UInt8]>) {
            var result = ""
            var codepoint:UInt32 = 0
            while let byte = utf8stream.next() where byte != 0x00 {
                codepoint = UInt32(byte)
                var extraBytes = 0
                if byte & 0b11100000 == 0b11000000 {
                    extraBytes = 1
                    codepoint &= 0b00011111
                }
                else if byte & 0b11110000 == 0b11100000 {
                    extraBytes = 2
                    codepoint &= 0b00001111
                }
                else if byte & 0b11111000 == 0b11110000 {
                    extraBytes = 3
                    codepoint &= 0b00000111
                }
                else if byte & 0b11111100 == 0b11111000 {
                    extraBytes = 4
                    codepoint &= 0b00000011
                }
                else if byte & 0b11111110 == 0b11111100 {
                    extraBytes = 5
                    codepoint &= 0b00000001
                }
                for _ in 0..

提交回复
热议问题