@@ -684,9 +684,17 @@ fileprivate func decodeTextArray(from buffer: UnsafeRawBufferPointer) -> Any? {
684684 defer { cursor += MemoryLayout< Int32> . size }
685685 return cursor. assumingMemoryBound ( to: Int32 . self) . pointee. bigEndian
686686 }
687-
687+ func readString( _ len: Int ) -> String ? {
688+ if len < 0 { return nil }
689+ let slice = UnsafeRawBufferPointer ( start: cursor, count: len)
690+ cursor += Int ( len)
691+ return String ( decoding: slice, as: UTF8 . self)
692+ }
693+
688694 // header
689695 let ndim = readInt32 ( )
696+ guard ndim != 0 else { return [ ] } // empty arrays have dim 0
697+
690698 let hasNull = readInt32 ( ) != 0 // flags, currently on 0 or 1?!
691699 let elemOID = readInt32 ( )
692700 assert ( elemOID == OIDs . TEXT)
@@ -695,6 +703,7 @@ fileprivate func decodeTextArray(from buffer: UnsafeRawBufferPointer) -> Any? {
695703 return nil
696704 }
697705
706+
698707 do { // one dimension
699708 let count = readInt32 ( )
700709 assert ( count >= 0 && count <= 1_000_000_000 )
@@ -707,31 +716,22 @@ fileprivate func decodeTextArray(from buffer: UnsafeRawBufferPointer) -> Any? {
707716 var result = [ String? ] ( ) ; result. reserveCapacity ( Int ( count) )
708717
709718 for _ in 0 ..< count {
710- let len = Int ( readInt32 ( ) . bigEndian)
711- if len < 0 {
712- result. append ( nil )
713- }
714- else {
715- let strData = UnsafeRawBufferPointer ( start: cursor, count: len)
716- cursor += len
717- result. append ( String ( decoding: strData, as: UTF8 . self) )
718- }
719+ let len = readInt32 ( )
720+ result. append ( readString ( Int ( len) ) )
719721 }
720722 return result
721723 }
722724 else {
723725 var result = [ String ] ( ) ; result. reserveCapacity ( Int ( count) )
724726
725727 for _ in 0 ..< count {
726- let len = Int ( readInt32 ( ) . bigEndian)
727- if len == - 1 {
728- assertionFailure ( " Array set to non-null, but contains nulls? " )
729- result. append ( " " )
728+ let len = readInt32 ( )
729+ if let s = readString ( Int ( len) ) {
730+ result. append ( s)
730731 }
731732 else {
732- let strData = UnsafeRawBufferPointer ( start: cursor, count: len)
733- cursor += len
734- result. append ( String ( decoding: strData, as: UTF8 . self) )
733+ assertionFailure ( " Array set to non-null, but contains nulls? " )
734+ result. append ( " " )
735735 }
736736 }
737737 return result
0 commit comments