Skip to content

Commit dd13eb9

Browse files
Gary Liujrose-apple
authored andcommitted
Endian fix for Strings in s390x (#4194)
1. The change in StaticString's withUTF8Buffer is to store the UTF-8 code unit properly in the buffer. 2. The change in StringCore's _nthContiguous is to represent the UTF-16 data correctly when it contains the high byte. We ran validation tests and foundation tests with the change on x86-64 and s390x. No new regression is observed.
1 parent b58c3a2 commit dd13eb9

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

stdlib/public/core/StaticString.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ public struct StaticString
132132
var buffer: UInt64 = 0
133133
var i = 0
134134
let sink: (UInt8) -> Void = {
135+
#if _endian(little)
135136
buffer = buffer | (UInt64($0) << (UInt64(i) * 8))
137+
#else
138+
buffer = buffer | (UInt64($0) << (UInt64(7-i) * 8))
139+
#endif
136140
i += 1
137141
}
138142
UTF8.encode(unicodeScalar, into: sink)

stdlib/public/core/StringCore.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,12 @@ public struct _StringCore {
299299
// Always dereference two bytes, but when elements are 8 bits we
300300
// multiply the high byte by 0.
301301
// FIXME(performance): use masking instead of multiplication.
302+
#if _endian(little)
302303
return UTF16.CodeUnit(p.pointee)
303304
+ UTF16.CodeUnit((p + 1).pointee) * _highByteMultiplier
305+
#else
306+
return _highByteMultiplier == 0 ? UTF16.CodeUnit(p.pointee) : UTF16.CodeUnit((p + 1).pointee) + UTF16.CodeUnit(p.pointee) * _highByteMultiplier
307+
#endif
304308
}
305309

306310
/// Get the Nth UTF-16 Code Unit stored.

0 commit comments

Comments
 (0)