github.com/primecitizens/pcz/std@v0.2.1/builtin/print/builtin.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 // 4 // Copyright 2009 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 package stdprint 9 10 import ( 11 "unsafe" 12 13 stdtype "github.com/primecitizens/pcz/std/builtin/type" 14 ) 15 16 func PrintPointer(p unsafe.Pointer) { PrintHex(uint64(uintptr(p))) } 17 func PrintUintptr(p uintptr) { PrintHex(uint64(p)) } 18 19 func PrintString(s string) { 20 if string_endpadding > 0 { 21 var ( 22 buf [128]byte 23 offset int 24 n int 25 ) 26 27 for offset < len(s) { 28 n = copy(buf[:127], s[offset:]) 29 if n != 127 { 30 break 31 } 32 33 gwrite(buf[:128]) 34 offset += n + 1 35 } 36 37 buf[n] = 0 // may have previous data left 38 gwrite(buf[:n+1]) 39 return 40 } 41 42 gwrite(unsafe.Slice(unsafe.StringData(s), len(s))) 43 } 44 45 func PrintEface(e stdtype.Eface) { 46 print(str_left_round_bracket, e.Type, str_comma, e.Data, str_right_round_bracket) 47 } 48 49 func PrintIface(i stdtype.Iface) { 50 print(str_left_round_bracket, i.Itab, str_comma, i.Data, str_right_round_bracket) 51 } 52 53 func PrintLock() { /* TODO */ } 54 func PrintUnlock() { /* TODO */ } 55 func PrintSpace() { PrintString(str_space) } 56 func PrintNewline() { PrintString(str_newline) } 57 func PrintComplex(c complex128) { 58 print(str_left_round_bracket, real(c), imag(c), str_i_right_round_bracket) 59 } 60 61 func PrintBool(v bool) { 62 if v { 63 PrintString(str_true) 64 } else { 65 PrintString(str_false) 66 } 67 } 68 69 func PrintFloat(v float64) { 70 switch { 71 case v != v: 72 PrintString(str_nan) 73 return 74 case v+v == v && v > 0: 75 PrintString(str_positive_inf) 76 return 77 case v+v == v && v < 0: 78 PrintString(str_negative_inf) 79 return 80 } 81 82 const n = 7 // digits printed 83 var buf [n + float_digits_ext]byte 84 buf[0] = '+' 85 e := 0 // exp 86 if v == 0 { 87 if 1/v < 0 { 88 buf[0] = '-' 89 } 90 } else { 91 if v < 0 { 92 v = -v 93 buf[0] = '-' 94 } 95 96 // normalize 97 for v >= 10 { 98 e++ 99 v /= 10 100 } 101 for v < 1 { 102 e-- 103 v *= 10 104 } 105 106 // round 107 h := 5.0 108 for i := 0; i < n; i++ { 109 h /= 10 110 } 111 v += h 112 if v >= 10 { 113 e++ 114 v /= 10 115 } 116 } 117 118 // format +d.dddd+edd 119 for i := 0; i < n; i++ { 120 s := int(v) 121 buf[i+2] = byte(s + '0') 122 v -= float64(s) 123 v *= 10 124 } 125 buf[1] = buf[2] 126 buf[2] = '.' 127 128 buf[n+2] = 'e' 129 buf[n+3] = '+' 130 if e < 0 { 131 e = -e 132 buf[n+3] = '-' 133 } 134 135 buf[n+4] = byte(e/100) + '0' 136 buf[n+5] = byte(e/10)%10 + '0' 137 buf[n+6] = byte(e%10) + '0' 138 gwrite(buf[:]) 139 } 140 141 func PrintUint(v uint64) { 142 var buf [100]byte 143 i := len(buf) 144 for i--; i > 0; i-- { 145 buf[i] = byte(v%10 + '0') 146 if v < 10 { 147 break 148 } 149 v /= 10 150 } 151 gwrite(buf[i:]) 152 } 153 154 func PrintInt(v int64) { 155 if v < 0 { 156 PrintString(str_minus) 157 v = -v 158 } 159 PrintUint(uint64(v)) 160 } 161 162 const minhexdigits = 0 163 164 func PrintHex(v uint64) { 165 const dig = "0123456789abcdef" 166 var buf [100]byte 167 i := len(buf) - uint_padding 168 for i--; i > 0; i-- { 169 buf[i] = dig[v%16] 170 if v < 16 && len(buf)-i >= minhexdigits { 171 break 172 } 173 v /= 16 174 } 175 i-- 176 buf[i] = 'x' 177 i-- 178 buf[i] = '0' 179 gwrite(buf[i:]) 180 } 181 182 func PrintSlice(s []byte) { 183 print(str_left_square_bracket, len(s), str_slash, cap(s), str_right_square_bracket) 184 PrintPointer(unsafe.Pointer(unsafe.SliceData(s))) 185 }