Use single-line if_true/if_false where possible

This makes the code more compact, and consistent with other single line
closures (e.g. single-line if() expressions). In a few cases the code is
left as-is to make things more readable.
parent 1aac792c
Pipeline #141553363 failed with stages
in 74 minutes and 52 seconds
......@@ -193,9 +193,7 @@ impl Array {
# Array.new(10, 20, 30).contains?(10) # => True
def contains?(value: T) -> Boolean where T: Equal {
each do (val) {
(val == value).if_true {
return True
}
(val == value).if_true { return True }
}
False
......@@ -312,16 +310,12 @@ impl Equal for Array {
#
# Array.new(10, 20) == Array.new(20, 10) # => False
def ==(other: Self) -> Boolean where T: Equal {
(length == other.length).if_false {
return False
}
(length == other.length).if_false { return False }
each_with_index do (ours: T, index: Integer) {
let theirs = other[index]!
(ours == theirs).if_false {
return False
}
(ours == theirs).if_false { return False }
}
True
......
......@@ -63,9 +63,7 @@ impl Array {
let buffer = StringBuffer.new
each_with_index do (value, index) {
index.positive?.if_true {
buffer.push(separator)
}
index.positive?.if_true { buffer.push(separator) }
buffer.push(value.to_string)
}
......
......@@ -202,10 +202,7 @@ impl Block {
# let mut number = 0
#
# {
# number == 100
# .if_true {
# return
# }
# (number == 100).if_true { return }
#
# number += 1
# }.loop
......
......@@ -207,9 +207,7 @@ impl ByteArray {
let mut until = index + length
(until >= self.length).if_true {
until = self.length
}
(until >= self.length).if_true { until = self.length }
{ index < until }.while_true {
new_array.push(self[index]!)
......@@ -238,9 +236,7 @@ impl Index!(Integer, Integer) for ByteArray {
#
# bytes[5] # => Nil
def [](index: Integer) -> ?Integer {
(index >= length).if_true {
return Nil
}
(index >= length).if_true { return }
_INKOC.byte_array_at(self, index)
}
......
......@@ -80,16 +80,12 @@ let MAX_HASH_VALUE = 21
def keyword?(bytes: ByteArray) -> Boolean {
let length = bytes.length
WORD_LENGTH.cover?(length).if_false {
return False
}
WORD_LENGTH.cover?(length).if_false { return False }
let hash =
length + ASSOCIATED_VALUES[bytes[1]!]! + ASSOCIATED_VALUES[bytes[0]!]!
(hash > MAX_HASH_VALUE).if_true {
return False
}
(hash > MAX_HASH_VALUE).if_true { return False }
WORD_LIST[hash] == bytes
}
......@@ -112,9 +112,7 @@ def last_call_frame_in(file: ToPath) -> CallFrame {
{ index >= 0 }.while_true {
let frame = frames[index]!
(frame.path == path).if_true {
return frame
}
(frame.path == path).if_true { return frame }
index -= 1
}
......
......@@ -76,9 +76,7 @@ def variables -> Map!(String, String) {
names.each do (name) {
let value = ThisModule[name]
value.if_true {
map[name] = value!
}
value.if_true { map[name] = value! }
}
map
......
......@@ -334,9 +334,7 @@ object Layout {
def [](name: String) -> Member {
let member = @members[name]
member.if_false {
process.panic('Undefined struct member: ' + name)
}
member.if_false { process.panic('Undefined struct member: ' + name) }
member!
}
......@@ -433,9 +431,7 @@ object LayoutBuilder {
# Creates a new `Layout` based on the current state of this builder.
def to_layout -> Layout {
@padding.if_true {
return layout_with_padding
}
@padding.if_true { return layout_with_padding }
layout_without_padding
}
......@@ -469,9 +465,7 @@ object LayoutBuilder {
size += type_align
offset += type_align
remaining_in_hole.zero?.if_true {
remaining_in_hole = @alignment
}
remaining_in_hole.zero?.if_true { remaining_in_hole = @alignment }
}
Layout.new(members: members, alignment: @alignment, size: size)
......@@ -515,9 +509,7 @@ impl SetIndex!(String, Type) for LayoutBuilder {
# alignment of the largest field.
@padding
.and { type.alignment > @alignment }
.if_true {
@alignment = type.alignment
}
.if_true { @alignment = type.alignment }
type
}
......
......@@ -84,25 +84,15 @@ impl Numeric for Float {
}
def absolute -> Self {
negative?.if_false {
return self
}
infinite?.if_true {
return INFINITY
}
negative?.if_false { return self }
infinite?.if_true { return INFINITY }
self - (self * 2.0)
}
def opposite -> Self {
positive?.if_true {
return 0.0 - self
}
infinite?.if_true {
return INFINITY
}
positive?.if_true { return 0.0 - self }
infinite?.if_true { return INFINITY }
self - (self * 2.0)
}
......@@ -272,13 +262,8 @@ impl Float {
#
# try! Float.parse('1.2e1') # => 12.0
static def parse(string: String) !! StandardError -> Float {
(string == INFINITY_LABEL).if_true {
return INFINITY
}
(string == NEGATIVE_INFINITY_LABEL).if_true {
return NEGATIVE_INFINITY
}
(string == INFINITY_LABEL).if_true { return INFINITY }
(string == NEGATIVE_INFINITY_LABEL).if_true { return NEGATIVE_INFINITY }
try {
_INKOC.string_to_float(string)
......@@ -330,16 +315,12 @@ impl ToString for Float {
# INFINITY.to_string # => 'INFINITY'
def to_string -> String {
infinite?.if_true {
positive?.if_true {
return INFINITY_LABEL
}
positive?.if_true { return INFINITY_LABEL }
return NEGATIVE_INFINITY_LABEL
}
not_a_number?.if_true {
return 'NaN'
}
not_a_number?.if_true { return 'NaN' }
_INKOC.float_to_string(self)
}
......
......@@ -187,15 +187,11 @@ object Path {
let path_str = path.to_string
let join_with = Path.new(path_str)
join_with.absolute?.if_true {
return join_with
}
join_with.absolute?.if_true { return join_with }
let buffer = StringBuffer.new(@path)
@path.ends_with?(SEPARATOR).if_false {
buffer.push(SEPARATOR)
}
@path.ends_with?(SEPARATOR).if_false { buffer.push(SEPARATOR) }
buffer.push(path_str)
......@@ -223,9 +219,7 @@ object Path {
def directory -> Path {
let length = bits.bytes_before_last_separator(@path)
length.negative?.if_true {
return Path.new('.')
}
length.negative?.if_true { return Path.new('.') }
Path.new(@path.slice_bytes(start: 0, length: length).drain_to_string)
}
......
......@@ -33,9 +33,7 @@ let COLON_BYTE = 58
def starts_with_windows_drive_name?(path: String) -> Boolean {
let first_byte = path.byte(index: 0)
(path.length < 3).if_true {
return False
}
(path.length < 3).if_true { return False }
WINDOWS_LOWER_DRIVE_LETTERS.cover?(first_byte)
.or { WINDOWS_UPPER_DRIVE_LETTERS.cover?(first_byte) }
......@@ -52,9 +50,7 @@ def path_separator?(byte: Integer) -> Boolean {
#
# If no separator could be found, `-1` is returned.
def bytes_before_last_separator(path: String) -> Integer {
path.empty?.if_true {
return -1
}
path.empty?.if_true { return -1 }
let windows_drive_path =
os.windows?.and { starts_with_windows_drive_name?(path) }
......@@ -82,12 +78,9 @@ def bytes_before_last_separator(path: String) -> Integer {
true: { in_separator = True },
false: {
in_separator.if_true {
windows_drive_path.and { index == 1 }.if_true {
# We have reached the ":" in a drive name such as "C:\". In this
# case we want to include the "\" since it's part of the drive
# name.
return 3
}
# We have reached the ":" in a drive name such as "C:\". In this case
# we want to include the "\" since it's part of the drive name.
windows_drive_path.and { index == 1 }.if_true { return 3 }
return index + 1
}
......
......@@ -128,17 +128,13 @@ impl Numeric for Integer {
}
def absolute -> Self {
negative?.if_false {
return self
}
negative?.if_false { return self }
self - (self * 2)
}
def opposite -> Self {
positive?.if_true {
return 0 - self
}
positive?.if_true { return 0 - self }
self - (self * 2)
}
......
......@@ -33,16 +33,12 @@ impl Integer {
process.panic('The radix argument must be between 2 and 36')
}
zero?.if_true {
return '0'
}
zero?.if_true { return '0' }
let characters = Array.new
let mut integer = absolute
negative?.if_true {
characters.push('-')
}
negative?.if_true { characters.push('-') }
{ integer.positive? }.while_true {
characters.push(INTEGER_RADIX_DIGITS[integer % radix]!)
......
......@@ -242,9 +242,7 @@ trait Iterator!(T) {
# numbers.iter.find do (number) { number > 50 } # => 80
def find(block: do (T) -> Boolean) -> ?T {
each do (value) {
block.call(value).if_true {
return value
}
block.call(value).if_true { return value }
}
Nil
......@@ -262,9 +260,7 @@ trait Iterator!(T) {
# Array.new(10, 20, 30).iter.any? do (value) { value >= 20 } # => True
def any?(block: do (T) -> Boolean) -> Boolean {
each do (value) {
block.call(value).if_true {
return True
}
block.call(value).if_true { return True }
}
False
......@@ -291,9 +287,7 @@ trait Iterator!(T) {
{ next?.and { (found as ?Object).nil? } }.while_true {
let value = next
block.call(value).if_true {
found = value
}
block.call(value).if_true { found = value }
}
(found as ?Object).not_nil?
......
......@@ -177,9 +177,7 @@ object Map!(K: Hash + Equal, V) {
}
keys.each_with_index do (key: K, index: Integer) {
(index > max_index).if_true {
return map
}
(index > max_index).if_true { return map }
map[key] = values[index]!
}
......@@ -221,9 +219,7 @@ object Map!(K: Hash + Equal, V) {
def remove(key: K) -> ?V {
let index = bucket_index(key)
index.if_false {
return
}
index.if_false { return }
let pair = @buckets[index!]
......@@ -362,9 +358,7 @@ object Map!(K: Hash + Equal, V) {
#
# let map = Map.new.set('a', 10).set('b', 20)
def set(key: K, value: V) -> Self {
(@length >= @resize_threshold).if_true {
rehash
}
(@length >= @resize_threshold).if_true { rehash }
insert_pair(Pair.new(key: key, value: value, hash: hash_key(key)))
......@@ -478,16 +472,12 @@ object Map!(K: Hash + Equal, V) {
#
# This early return ensures we don't iterate over all buckets if we are
# certain we won't be able to find the key.
pair.nil?.if_true {
return
}
pair.nil?.if_true { return }
index = desired_bucket(index + 1)
(index == desired).if_true {
# We cycled through all buckets but didn't find a matching pair.
return
}
# We cycled through all buckets but didn't find a matching pair.
(index == desired).if_true { return }
pair = @buckets[index]
}
......@@ -529,18 +519,12 @@ impl Equal for Map {
#
# map1 == map2 # => True
def ==(other: Self) -> Boolean where V: Equal {
(length == other.length).if_false {
return False
}
(length == other.length).if_false { return False }
each do (key, value) {
other.key?(key).if_false {
return False
}
other.key?(key).if_false { return False }
(value == other[key]!).if_false {
return False
}
(value == other[key]!).if_false { return False }
}
True
......
......@@ -198,15 +198,11 @@ trait Mirror: Format {
let mut proto = _INKOC.get_prototype(subject)
{
proto.equal?(other).if_true {
return True
}
proto.equal?(other).if_true { return True }
proto = _INKOC.get_prototype(proto) as Object
proto.nil?.if_true {
return False
}
proto.nil?.if_true { return False }
}.loop
}
......@@ -234,9 +230,7 @@ trait Mirror: Format {
let traits = _INKOC.get_attribute(subject, IMPLEMENTED_TRAITS_ATTRIBUTE)
traits.if_true {
_INKOC.get_attribute(traits, find).if_true {
return True
}
_INKOC.get_attribute(traits, find).if_true { return True }
}
subject = _INKOC.get_prototype(subject) as Object
......@@ -246,9 +240,7 @@ trait Mirror: Format {
# and "the prototype is Nil", we will just bail out once we hit Nil as the
# prototype. This works fine for Nil itself, because there is only a
# single instance of it.
subject.nil?.if_true {
return False
}
subject.nil?.if_true { return False }
}.loop
}
......@@ -271,9 +263,7 @@ trait Mirror: Format {
def implemented_traits -> Array!(Trait) {
let impl_set = get_attribute(IMPLEMENTED_TRAITS_ATTRIBUTE) as Object
impl_set.if_false {
return Array.new
}
impl_set.if_false { return Array.new }
_INKOC.get_attribute_names(impl_set) as Dynamic as Array!(Trait)
}
......@@ -371,16 +361,12 @@ impl Format for ObjectMirror {
formatter.push(name)
attributes.empty?.if_true {
return formatter
}
attributes.empty?.if_true { return formatter }
formatter.push(' {')
attributes.each_with_index do (attr_name, index) {
index.positive?.if_true {
formatter.push(',')
}
index.positive?.if_true { formatter.push(',') }
formatter.push(' ')
formatter.push(attr_name)
......@@ -579,16 +565,12 @@ impl Format for ArrayMirror {
def format!(F: Formatter)(formatter: F) -> F where T: Mirrored {
formatter.push('Array')
@subject.empty?.if_true {
return formatter
}
@subject.empty?.if_true { return formatter }
formatter.push(' { ')
@subject.each_with_index do (value, index) {
index.positive?.if_true {
formatter.push(', ')
}
index.positive?.if_true { formatter.push(', ') }
formatter.descend {
value.mirror.format(formatter)
......@@ -647,16 +629,12 @@ impl Format for MapMirror {
formatter.push('Map')
@subject.empty?.if_true {
return formatter
}
@subject.empty?.if_true { return formatter }
formatter.push(' { ')
@subject.each do (key, value) {
index.positive?.if_true {
formatter.push(', ')
}
index.positive?.if_true { formatter.push(', ') }
formatter.descend {
key.mirror.format(formatter)
......@@ -698,16 +676,12 @@ impl Format for SetMirror {
formatter.push('Set')
@subject.empty?.if_true {
return formatter
}
@subject.empty?.if_true { return formatter }
formatter.push(' { ')
@subject.each do (value) {
index.positive?.if_true {
formatter.push(', ')
}
index.positive?.if_true { formatter.push(', ') }
formatter.descend {
value.mirror.format(formatter)
......
......@@ -149,13 +149,8 @@ impl ToString for Ipv6Address {
# Ipv6Address.new.to_string # => '::'
# Ipv6Address.new(0, 0, 0, 0, 0, 0, 0, 1) # => '::1'
def to_string -> String {
unspecified?.if_true {
return '::'
}
loopback?.if_true {
return '::1'
}
unspecified?.if_true { return '::' }
loopback?.if_true { return '::1' }
let ipv4_compatible = ipv4_compatible?
let ipv4_mapped = ipv4_mapped?
......@@ -178,9 +173,7 @@ impl ToString for Ipv6Address {
)
.to_string
ipv4_compatible.if_true {
return '::' + ipv4_address
}
ipv4_compatible.if_true { return '::' + ipv4_address }
return '::ffff:' + ipv4_address
}
......@@ -195,9 +188,7 @@ impl ToString for Ipv6Address {
@segments.each_with_index do (hextet, index) {
hextet.zero?.if(
true: {
current_len.zero?.if_true {
current_at = index
}
current_len.zero?.if_true { current_at = index }
current_len += 1
......@@ -220,14 +211,10 @@ impl ToString for Ipv6Address {
let compression_end = compression_start + compression_len
@segments.each_with_index do (hextet, index) {
(index == compression_start).if_true {
buffer.push(':')
}
(index == compression_start).if_true { buffer.push(':') }
(index < compression_start).or { index >= compression_end }.if_true {
index.positive?.if_true {
buffer.push(':')
}
index.positive?.if_true { buffer.push(':') }
buffer.push(hextet.format(radix: 16))
}
......@@ -239,9 +226,7 @@ impl ToString for Ipv6Address {
@segments.each_with_index do (hextet, index) {
buffer.push(hextet.format(radix: 16))
(index < 7).if_true {
buffer.push(':')
}
(index < 7).if_true { buffer.push(':') }
}
}
)
......@@ -361,9 +346,7 @@ impl IpAddress for Ipv6Address {
}.while_true {
let byte = bytes[pad_cursor]
(byte == _COLON_BYTE).if_true {
pad -= 1
}
(byte == _COLON_BYTE).if_true { pad -= 1 }
# Two IPv4 octets can be stored in a single IPv6 hextet, meaning we'd
# have to reduce padding by two. Since we already skip padding for the
......@@ -614,9 +597,7 @@ impl IpAddress for Ipv4Address {
let segment_bytes = ByteArray.new
# No IPv4 address can be longer than 15 characters (255.255.255.255).
(max > 15).if_true {
throw AddressParseError.new(bytes)
}