Skip to content

Commit 8fb0b5e

Browse files
author
Max Moiseev
committed
[stdlib] Implementing var words in terms of _word(at:)
1 parent 2750f59 commit 8fb0b5e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

stdlib/public/core/Integers.swift.gyb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,12 @@ public protocol BinaryInteger :
14761476
/// n-th word of this value.
14771477
func _word(at n: Int) -> UInt
14781478

1479+
// FIXME(integers): add doc comments
1480+
// FIXME: Should be `Words : Collection where Words.Iterator.Element == UInt`
1481+
// See <rdar://problem/31798916> for why it isn't.
1482+
associatedtype Words
1483+
var words: Words { get }
1484+
14791485
/// The number of bits in the current binary representation of this value.
14801486
///
14811487
/// This property is a constant for instances of fixed-width integer
@@ -1918,6 +1924,18 @@ extension BinaryInteger {
19181924
}
19191925
#endif
19201926

1927+
extension BinaryInteger {
1928+
// FIXME(integers): inefficient. Should get rid of _word(at:) and
1929+
// countRepresentedWords, and make `words` the basic operation.
1930+
public var words: [UInt] {
1931+
var result = [UInt]()
1932+
result.reserveCapacity(countRepresentedWords)
1933+
for i in 0..<self.countRepresentedWords {
1934+
result.append(_word(at: i))
1935+
}
1936+
return result
1937+
}
1938+
}
19211939

19221940
//===----------------------------------------------------------------------===//
19231941
//===--- FixedWidthInteger ------------------------------------------------===//

test/stdlib/Integers.swift.gyb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,16 @@ tests.test("word") {
506506
expectEqual(Int.min._lowUWord, z._word(at: 0))
507507
expectEqual(0, z._word(at: 1))
508508
}
509+
510+
tests.test("words") {
511+
expectEqualSequence([UInt.max], (-1 as Int).words)
512+
expectEqualSequence([UInt.max], (-1 as Int8).words)
513+
expectEqualSequence([UInt.max], (-1 as Int16).words)
514+
expectEqualSequence([UInt.max], (-1 as Int32).words)
515+
expectEqualSequence([UInt.max], (-1 as Int64).words)
516+
517+
expectEqualSequence([1], 1.words)
518+
expectEqualSequence([0], 0.words)
509519
}
510520

511521
tests.test("multipliedFullWidth/UInt8") {

0 commit comments

Comments
 (0)