nish 0.33.0 → 0.34.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -445,13 +445,56 @@ const pgBytesOf = (text: string): u8[] => {
445
445
 
446
446
  /** How many bytes `value` takes in decimal, its sign included. */
447
447
  const pgDecimalLength = (value: i64): i32 => {
448
+ // Counted on the negative side, where the most negative i64 fits, two digits a step.
448
449
  let n: i32 = value < 0 ? 2 : 1
449
450
  let v: i64 = value < 0 ? value : -value
450
- while (v <= -10) {
451
- v = v / 10
452
- n += 1
451
+ while (v <= -100) {
452
+ v = v / 100
453
+ n += 2
453
454
  }
454
- return n
455
+ return v <= -10 ? n + 1 : n
456
+ }
457
+
458
+ /**
459
+ * Writes `value` in decimal into `out[at, at + n)`, where `n` is
460
+ * `pgDecimalLength(value)`, and answers `at + n`. The digits go once, into
461
+ * their places, from the last, two a step from the magnitude as a `u64`
462
+ * (which holds the most negative i64's, and whose `/` and `%` are never
463
+ * checked). Panics unless the window is inside `out`; each step's guard
464
+ * restates it in the shape that proves its two stores.
465
+ */
466
+ const pgPutDecimal = (out: u8[], at: i32, value: i64, n: i32): i32 => {
467
+ if (!(at >= 0 && at < 2147483620 && n >= 1 && n <= 20 && at + n <= toI32(out.length))) {
468
+ panic("nish/net/postgres: a decimal outside its room")
469
+ }
470
+ const first: i32 = value < 0 ? at + 1 : at
471
+ let m: u64 = value < 0 ? toU64(0) - toU64(value) : toU64(value)
472
+ // Where the last two digits start.
473
+ let j: i32 = at + n - 2
474
+ while (m >= 100 && j >= first && j >= 0 && j < 2147483640 && j + 2 <= toI32(out.length)) {
475
+ const pair: u32 = toU32(m % 100)
476
+ const tens: u32 = pair / 10
477
+ out[j] = toU8(48 + tens)
478
+ out[j + 1] = toU8(48 + pair - tens * 10)
479
+ m = m / 100
480
+ j -= 2
481
+ }
482
+ // One or two digits are left, ending at `j + 2`.
483
+ const pair: u32 = toU32(m)
484
+ if (pair >= 10 && j >= first && j >= 0 && j < 2147483640 && j + 2 <= toI32(out.length)) {
485
+ const tens: u32 = pair / 10
486
+ out[j] = toU8(48 + tens)
487
+ out[j + 1] = toU8(48 + pair - tens * 10)
488
+ } else {
489
+ const last: i32 = j + 1
490
+ if (last >= first && last >= 0 && last < toI32(out.length)) {
491
+ out[last] = toU8(48 + pair)
492
+ }
493
+ }
494
+ if (value < 0 && at < toI32(out.length)) {
495
+ out[at] = toU8(45)
496
+ }
497
+ return at + n
455
498
  }
456
499
 
457
500
  /** Whether `value` is an `int4`, -2^31..2^31-1. */
@@ -2274,32 +2317,9 @@ export class PgConnection {
2274
2317
 
2275
2318
  /** `value` in decimal, `pgDecimalLength(value)` bytes. */
2276
2319
  private putDecimal(value: i64): void {
2277
- this.reserve(this.outEnd + 20)
2278
- // The digits are written from the least significant, then reversed; the
2279
- // value is made negative first so that the most negative i64 has its digits.
2280
- let v: i64 = value < 0 ? value : -value
2281
- if (value < 0) {
2282
- this.out[this.outEnd] = toU8(45)
2283
- this.outEnd += 1
2284
- }
2285
- const first: i32 = this.outEnd
2286
- while (true) {
2287
- this.out[this.outEnd] = toU8(48 - toI32(v % 10))
2288
- this.outEnd += 1
2289
- v = v / 10
2290
- if (v === 0) {
2291
- break
2292
- }
2293
- }
2294
- let lo: i32 = first
2295
- let hi: i32 = this.outEnd - 1
2296
- while (lo < hi) {
2297
- const t: u8 = this.out[lo]
2298
- this.out[lo] = this.out[hi]
2299
- this.out[hi] = t
2300
- lo++
2301
- hi--
2302
- }
2320
+ const n: i32 = pgDecimalLength(value)
2321
+ this.reserve(this.outEnd + n)
2322
+ this.outEnd = pgPutDecimal(this.out, this.outEnd, value, n)
2303
2323
  }
2304
2324
 
2305
2325
  /**