Skip to content

Commit 9d4db54

Browse files
authored
Remove the floorMod and floorDiv functions (#212)
1 parent f340a2b commit 9d4db54

File tree

7 files changed

+18
-109
lines changed

7 files changed

+18
-109
lines changed

core/js/src/Instant.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public actual class Instant internal constructor(internal val value: jtInstant)
9393
/* Performing normalization here because otherwise this fails:
9494
assertEquals((Long.MAX_VALUE % 1_000_000_000).toInt(),
9595
Instant.fromEpochSeconds(0, Long.MAX_VALUE).nanosecondsOfSecond) */
96-
val secs = safeAdd(epochSeconds, floorDiv(nanosecondAdjustment, NANOS_PER_ONE.toLong()))
97-
val nos = floorMod(nanosecondAdjustment, NANOS_PER_ONE.toLong()).toInt()
96+
val secs = safeAdd(epochSeconds, nanosecondAdjustment.floorDiv(NANOS_PER_ONE.toLong()))
97+
val nos = nanosecondAdjustment.mod(NANOS_PER_ONE.toLong()).toInt()
9898
Instant(jtInstant.ofEpochSecond(secs, nos))
9999
} catch (e: Throwable) {
100100
if (!e.isJodaDateTimeException() && e !is ArithmeticException) throw e

core/js/src/mathJs.kt

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,4 @@ internal actual fun safeMultiply(a: Int, b: Int): Int {
5757
val result = a.toLong() * b
5858
if (result > Int.MAX_VALUE || result < Int.MIN_VALUE) throw ArithmeticException("Multiplication overflows Int range: $a * $b.")
5959
return result.toInt()
60-
}
61-
62-
/**
63-
* Returns the floor division.
64-
*
65-
* This returns `0` for `floorDiv(0, 4)`.
66-
* This returns `-1` for `floorDiv(-1, 4)`.
67-
* This returns `-1` for `floorDiv(-2, 4)`.
68-
* This returns `-1` for `floorDiv(-3, 4)`.
69-
* This returns `-1` for `floorDiv(-4, 4)`.
70-
* This returns `-2` for `floorDiv(-5, 4)`.
71-
*
72-
* @param a the dividend
73-
* @param b the divisor
74-
* @return the floor division
75-
*/
76-
internal fun floorDiv(a: Long, b: Long): Long = if (a >= 0) a / b else (a + 1) / b - 1
77-
78-
/**
79-
* Returns the floor modulus.
80-
*
81-
* This returns `0` for `floorMod(0, 4)`.
82-
* This returns `1` for `floorMod(-1, 4)`.
83-
* This returns `2` for `floorMod(-2, 4)`.
84-
* This returns `3` for `floorMod(-3, 4)`.
85-
* This returns `0` for `floorMod(-4, 4)`.
86-
*
87-
* @param a the dividend
88-
* @param b the divisor
89-
* @return the floor modulus (positive)
90-
*/
91-
internal fun floorMod(a: Long, b: Long): Long = (a % b + b) % b
60+
}

core/native/src/Instant.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,18 @@ public actual class Instant internal constructor(public actual val epochSeconds:
199199
public actual fun fromEpochMilliseconds(epochMilliseconds: Long): Instant =
200200
if (epochMilliseconds < MIN_SECOND * MILLIS_PER_ONE) MIN
201201
else if (epochMilliseconds > MAX_SECOND * MILLIS_PER_ONE) MAX
202-
else Instant(floorDiv(epochMilliseconds, MILLIS_PER_ONE.toLong()),
203-
(floorMod(epochMilliseconds, MILLIS_PER_ONE.toLong()) * NANOS_PER_MILLI).toInt())
202+
else Instant(
203+
epochMilliseconds.floorDiv(MILLIS_PER_ONE.toLong()),
204+
(epochMilliseconds.mod(MILLIS_PER_ONE.toLong()) * NANOS_PER_MILLI).toInt()
205+
)
204206

205207
/**
206208
* @throws ArithmeticException if arithmetic overflow occurs
207209
* @throws IllegalArgumentException if the boundaries of Instant are overflown
208210
*/
209211
private fun fromEpochSecondsThrowing(epochSeconds: Long, nanosecondAdjustment: Long): Instant {
210-
val secs = safeAdd(epochSeconds, floorDiv(nanosecondAdjustment, NANOS_PER_ONE.toLong()))
211-
val nos = floorMod(nanosecondAdjustment, NANOS_PER_ONE.toLong()).toInt()
212+
val secs = safeAdd(epochSeconds, nanosecondAdjustment.floorDiv(NANOS_PER_ONE.toLong()))
213+
val nos = nanosecondAdjustment.mod(NANOS_PER_ONE.toLong()).toInt()
212214
return Instant(secs, nos)
213215
}
214216

@@ -333,8 +335,8 @@ internal actual fun Instant.toStringWithOffset(offset: UtcOffset): String {
333335
val seconds = epochSeconds + offset.totalSeconds
334336
if (seconds >= -SECONDS_0000_TO_1970) { // current era
335337
val zeroSecs: Long = seconds - SECONDS_PER_10000_YEARS + SECONDS_0000_TO_1970
336-
val hi: Long = floorDiv(zeroSecs, SECONDS_PER_10000_YEARS) + 1
337-
val lo: Long = floorMod(zeroSecs, SECONDS_PER_10000_YEARS)
338+
val hi: Long = zeroSecs.floorDiv(SECONDS_PER_10000_YEARS) + 1
339+
val lo: Long = zeroSecs.mod(SECONDS_PER_10000_YEARS)
338340
val ldt: LocalDateTime = Instant(lo - SECONDS_0000_TO_1970, 0)
339341
.toLocalDateTime(TimeZone.UTC)
340342
if (hi > 0) {

core/native/src/LocalDate.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public actual class LocalDate actual constructor(public actual val year: Int, pu
140140
// org.threeten.bp.LocalDate#getDayOfWeek
141141
public actual val dayOfWeek: DayOfWeek
142142
get() {
143-
val dow0 = floorMod(toEpochDay() + 3, 7)
143+
val dow0 = (toEpochDay() + 3).mod(7)
144144
return DayOfWeek(dow0 + 1)
145145
}
146146

@@ -190,8 +190,8 @@ public actual class LocalDate actual constructor(public actual val year: Int, pu
190190
}
191191
val monthCount = year * 12 + (monthNumber - 1)
192192
val calcMonths = safeAdd(monthCount, monthsToAdd)
193-
val newYear = floorDiv(calcMonths, 12)
194-
val newMonth = floorMod(calcMonths, 12) + 1
193+
val newYear = calcMonths.floorDiv(12)
194+
val newMonth = calcMonths.mod(12) + 1
195195
return resolvePreviousValid(newYear, newMonth, dayOfMonth)
196196
}
197197

core/native/src/LocalDateTime.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ internal fun LocalDateTime.plusSeconds(seconds: Int): LocalDateTime
121121
val totalNanos: Long = seconds % SECONDS_PER_DAY * NANOS_PER_ONE.toLong() + // at most a day
122122
currentNanoOfDay
123123
val totalDays = seconds / SECONDS_PER_DAY + // max/24*60*60 < max * 0.000012
124-
floorDiv(totalNanos, NANOS_PER_DAY) // max 2 days
125-
val newNanoOfDay: Long = floorMod(totalNanos, NANOS_PER_DAY)
124+
totalNanos.floorDiv(NANOS_PER_DAY) // max 2 days
125+
val newNanoOfDay: Long = totalNanos.mod(NANOS_PER_DAY)
126126
val newTime: LocalTime = if (newNanoOfDay == currentNanoOfDay) time else LocalTime.ofNanoOfDay(newNanoOfDay)
127127
return LocalDateTime(date.plusDays(totalDays.toInt()), newTime)
128128
}

core/native/src/TimeZone.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ internal actual fun Instant.toLocalDateTime(offset: UtcOffset): LocalDateTime =
143143

144144
internal fun Instant.toLocalDateTimeImpl(offset: UtcOffset): LocalDateTime {
145145
val localSecond: Long = epochSeconds + offset.totalSeconds // overflow caught later
146-
val localEpochDay = floorDiv(localSecond, SECONDS_PER_DAY.toLong()).toInt()
147-
val secsOfDay = floorMod(localSecond, SECONDS_PER_DAY.toLong()).toInt()
146+
val localEpochDay = localSecond.floorDiv(SECONDS_PER_DAY.toLong()).toInt()
147+
val secsOfDay = localSecond.mod(SECONDS_PER_DAY.toLong()).toInt()
148148
val date: LocalDate = LocalDate.ofEpochDay(localEpochDay) // may throw
149149
val time: LocalTime = LocalTime.ofSecondOfDay(secsOfDay, nanosecondsOfSecond)
150150
return LocalDateTime(date, time)

core/native/src/mathNative.kt

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -134,68 +134,6 @@ internal actual fun safeMultiply(a: Int, b: Int): Int {
134134
return total.toInt()
135135
}
136136

137-
/**
138-
* Returns the floor division.
139-
*
140-
* This returns `0` for `floorDiv(0, 4)`.
141-
* This returns `-1` for `floorDiv(-1, 4)`.
142-
* This returns `-1` for `floorDiv(-2, 4)`.
143-
* This returns `-1` for `floorDiv(-3, 4)`.
144-
* This returns `-1` for `floorDiv(-4, 4)`.
145-
* This returns `-2` for `floorDiv(-5, 4)`.
146-
*
147-
* @param a the dividend
148-
* @param b the divisor
149-
* @return the floor division
150-
*/
151-
internal fun floorDiv(a: Long, b: Long): Long = if (a >= 0) a / b else (a + 1) / b - 1
152-
153-
/**
154-
* Returns the floor division.
155-
*
156-
* This returns `0` for `floorDiv(0, 4)`.
157-
* This returns `-1` for `floorDiv(-1, 4)`.
158-
* This returns `-1` for `floorDiv(-2, 4)`.
159-
* This returns `-1` for `floorDiv(-3, 4)`.
160-
* This returns `-1` for `floorDiv(-4, 4)`.
161-
* This returns `-2` for `floorDiv(-5, 4)`.
162-
*
163-
* @param a the dividend
164-
* @param b the divisor
165-
* @return the floor division
166-
*/
167-
internal fun floorDiv(a: Int, b: Int): Int = if (a >= 0) a / b else (a + 1) / b - 1
168-
169-
/**
170-
* Returns the floor modulus.
171-
*
172-
* This returns `0` for `floorMod(0, 4)`.
173-
* This returns `1` for `floorMod(-1, 4)`.
174-
* This returns `2` for `floorMod(-2, 4)`.
175-
* This returns `3` for `floorMod(-3, 4)`.
176-
* This returns `0` for `floorMod(-4, 4)`.
177-
*
178-
* @param a the dividend
179-
* @param b the divisor
180-
* @return the floor modulus (positive)
181-
*/
182-
internal fun floorMod(a: Long, b: Long): Long = (a % b + b) % b
183-
184-
/**
185-
* Returns the floor modulus.
186-
*
187-
* This returns `0` for `floorMod(0, 4)`.
188-
* This returns `1` for `floorMod(-1, 4)`.
189-
* This returns `2` for `floorMod(-2, 4)`.
190-
* This returns `3` for `floorMod(-3, 4)`.
191-
* This returns `0` for `floorMod(-4, 4)`.
192-
*
193-
* @param a the dividend
194-
* @param b the divisor
195-
* @return the floor modulus (positive)
196-
*/
197-
internal fun floorMod(a: Int, b: Int): Int = (a % b + b) % b
198-
199137
// org.threeten.bp.ZoneOffset#buildId
200138
internal fun zoneIdByOffset(totalSeconds: Int): String {
201139
return if (totalSeconds == 0) {

0 commit comments

Comments
 (0)