Debian Codegraph @

es6numfmt.go

raw symbols SCIP
1 // Copyright 2021 Bret Jordan & Benedikt Thoma, All rights reserved.
2 // Copyright 2006-2019 WebPKI.org (http://webpki.org).
3 //
4 // Use of this source code is governed by an Apache 2.0 license that can be
5 // found in the LICENSE file in the root of the source tree.
6
7 package jcsscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/
8
9 import (
10 "errorsscip-go gomod github.com/golang/go/src go1.15 errors/"
11 "mathscip-go gomod github.com/golang/go/src go1.15 math/"
12 "strconvscip-go gomod github.com/golang/go/src go1.15 strconv/"
13 "stringsscip-go gomod github.com/golang/go/src go1.15 strings/"
14 )
15
16 const invalidPatternscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/invalidPattern. uint64 = 0x7ff0000000000000
17
18 // NumberToJSON converts numbers in IEEE-754 double precision into the
19 // format specified for JSON in EcmaScript Version 6 and forward.
20 // The core application for this is canonicalization per RFC 8785:
21 func NumberToJSONscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/NumberToJSON().(ieeeF64 float64) (res string, err error) {
22 ieeeU64 := mathscip-go gomod github.com/golang/go/src go1.15 math/.Float64bitsscip-go gomod github.com/golang/go/src go1.15 math/Float64bits().(ieeeF64)
23
24 // Special case: NaN and Infinity are invalid in JSON
25 if (ieeeU64 & invalidPatternscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/invalidPattern.) == invalidPatternscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/invalidPattern. {
26 return "null", errorsscip-go gomod github.com/golang/go/src go1.15 errors/.Newscip-go gomod github.com/golang/go/src go1.15 errors/New().("Invalid JSON number: " + strconvscip-go gomod github.com/golang/go/src go1.15 strconv/.FormatUintscip-go gomod github.com/golang/go/src go1.15 strconv/FormatUint().(ieeeU64, 16))
27 }
28
29 // Special case: eliminate "-0" as mandated by the ES6-JSON/JCS specifications
30 if ieeeF64 == 0 { // Right, this line takes both -0 and 0
31 return "0", nil
32 }
33
34 // Deal with the sign separately
35 var sign string = ""
36 if ieeeF64 < 0 {
37 ieeeF64 = -ieeeF64
38 sign = "-"
39 }
40
41 // ES6 has a unique "g" format
42 var format byte = 'e'
43 if ieeeF64 < 1e+21 && ieeeF64 >= 1e-6 {
44 format = 'f'
45 }
46
47 // The following should (in "theory") do the trick:
48 es6Formatted := strconvscip-go gomod github.com/golang/go/src go1.15 strconv/.FormatFloatscip-go gomod github.com/golang/go/src go1.15 strconv/FormatFloat().(ieeeF64, format, -1, 64)
49
50 // Ryu version
51 exponent := stringsscip-go gomod github.com/golang/go/src go1.15 strings/.IndexBytescip-go gomod github.com/golang/go/src go1.15 strings/IndexByte().(es6Formatted, 'e')
52 if exponent > 0 {
53 // Go outputs "1e+09" which must be rewritten as "1e+9"
54 if es6Formatted[exponent+2] == '0' {
55 es6Formatted = es6Formatted[:exponent+2] + es6Formatted[exponent+3:]
56 }
57 }
58 return sign + es6Formatted, nil
59 }