You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
3.1 KiB
152 lines
3.1 KiB
// ------------------------------------------------------------------------
|
|
// Project active2
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File name writer.go
|
|
// Created by DEV
|
|
// Modified 21/04/2024
|
|
//
|
|
// Copyright 2024 activething.com
|
|
// ------------------------------------------------------------------------
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
// ------------------------------------------------------------------------
|
|
|
|
package cjson
|
|
|
|
import (
|
|
"bfrs"
|
|
"cdcs/base32"
|
|
"cdcs/base64"
|
|
"time"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
|
|
func WriteString(b *bfrs.ABuffer, value string, escapeHTML bool ) {
|
|
if len(value) == 0 {
|
|
b.Write2Byte('"', '"')
|
|
return
|
|
}
|
|
|
|
b.WriteByte('"')
|
|
start := 0
|
|
for i := 0; i < len(value); {
|
|
if ch := value[i]; ch < utf8.RuneSelf {
|
|
|
|
if !G.JSONChars[ch].IsHtmlUnsafe() || (!escapeHTML && !G.JSONChars[ch].IsUnsafe()) {
|
|
i++
|
|
continue
|
|
}
|
|
|
|
if start < i {
|
|
b.WriteString(value[start:i])
|
|
}
|
|
b.WriteByte('\\')
|
|
switch ch {
|
|
case '\\', '"':
|
|
b.WriteByte(ch)
|
|
case '\n':
|
|
b.WriteByte('n')
|
|
case '\r':
|
|
b.WriteByte('r')
|
|
case '\t':
|
|
b.WriteByte('t')
|
|
default:
|
|
b.WriteString(`u00`)
|
|
b.Write2Byte(G.Hex[ch>>4], G.Hex[ch&0xF])
|
|
}
|
|
i++
|
|
start = i
|
|
continue
|
|
}
|
|
c, size := utf8.DecodeRuneInString(value[i:])
|
|
if c == utf8.RuneError && size == 1 {
|
|
if start < i {
|
|
b.WriteString(value[start:i])
|
|
}
|
|
b.WriteString(`\ufffd`)
|
|
i += size
|
|
start = i
|
|
continue
|
|
}
|
|
|
|
if c == '\u2028' || c == '\u2029' {
|
|
if start < i {
|
|
b.WriteString(value[start:i])
|
|
}
|
|
b.WriteString(`\u202`)
|
|
b.WriteByte(G.Hex[c&0xF])
|
|
i += size
|
|
start = i
|
|
continue
|
|
}
|
|
i += size
|
|
}
|
|
if start < len(value) {
|
|
b.WriteString(value[start:])
|
|
}
|
|
b.WriteByte('"')
|
|
}
|
|
|
|
|
|
|
|
func WriteUint (b *bfrs.ABuffer, value uint64) {
|
|
if value == 0 {
|
|
b.WriteByte('0')
|
|
return
|
|
}
|
|
b.WriteUIntStr(value, 10)
|
|
}
|
|
|
|
|
|
func WriteBool (b *bfrs.ABuffer, value bool) {
|
|
if value {
|
|
b.W(G.TrueValue)
|
|
return
|
|
}
|
|
b.W(G.FalseValue)
|
|
}
|
|
|
|
|
|
func WriteInt (b *bfrs.ABuffer, value int64) {
|
|
if value == 0 {
|
|
b.WriteByte('0')
|
|
} else {
|
|
b.WriteIntStr(value, 10)
|
|
}
|
|
}
|
|
|
|
|
|
func WriteFloat (b *bfrs.ABuffer, f float64, bits int) {
|
|
b.WriteString(JSONFloat(f,bits)) }
|
|
|
|
|
|
func WriteTime(b *bfrs.ABuffer, value time.Time, layout string) {
|
|
if layout == "" {
|
|
layout = time.RFC3339
|
|
}
|
|
b.WriteByte('"')
|
|
b.WriteDateStr(value,layout)
|
|
b.WriteByte('"')
|
|
}
|
|
|
|
|
|
func WriteBase64(b *bfrs.ABuffer, value []byte) (er error) {
|
|
return base64.Write(b,value,G.NullValue,G.EmptyValue,true,'"') }
|
|
|
|
|
|
func WriteBase32(b *bfrs.ABuffer, value []byte) (er error) {
|
|
return base32.Write(b,value,G.NullValue,G.EmptyValue,true,'"') }
|
|
|
|
|