gitee.com/quant1x/gox@v1.7.6/fastjson/arena.go (about)

     1  package fastjson
     2  
     3  import (
     4  	"strconv"
     5  )
     6  
     7  // Arena may be used for fast creation and re-use of Values.
     8  //
     9  // Typical Arena lifecycle:
    10  //
    11  //     1) Construct Values via the Arena and Value.Set* calls.
    12  //     2) Marshal the constructed Values with Value.MarshalTo call.
    13  //     3) Reset all the constructed Values at once by Arena.Reset call.
    14  //     4) Go to 1 and re-use the Arena.
    15  //
    16  // It is unsafe calling Arena methods from concurrent goroutines.
    17  // Use per-goroutine Arenas or ArenaPool instead.
    18  type Arena struct {
    19  	b []byte
    20  	c cache
    21  }
    22  
    23  // Reset resets all the Values allocated by a.
    24  //
    25  // Values previously allocated by a cannot be used after the Reset call.
    26  func (a *Arena) Reset() {
    27  	a.b = a.b[:0]
    28  	a.c.reset()
    29  }
    30  
    31  // NewObject returns new empty object value.
    32  //
    33  // New entries may be added to the returned object via Set call.
    34  //
    35  // The returned object is valid until Reset is called on a.
    36  func (a *Arena) NewObject() *Value {
    37  	v := a.c.getValue()
    38  	v.t = TypeObject
    39  	v.o.reset()
    40  	return v
    41  }
    42  
    43  // NewArray returns new empty array value.
    44  //
    45  // New entries may be added to the returned array via Set* calls.
    46  //
    47  // The returned array is valid until Reset is called on a.
    48  func (a *Arena) NewArray() *Value {
    49  	v := a.c.getValue()
    50  	v.t = TypeArray
    51  	v.a = v.a[:0]
    52  	return v
    53  }
    54  
    55  // NewString returns new string value containing s.
    56  //
    57  // The returned string is valid until Reset is called on a.
    58  func (a *Arena) NewString(s string) *Value {
    59  	v := a.c.getValue()
    60  	v.t = typeRawString
    61  	bLen := len(a.b)
    62  	a.b = escapeString(a.b, s)
    63  	v.s = b2s(a.b[bLen+1 : len(a.b)-1])
    64  	return v
    65  }
    66  
    67  // NewStringBytes returns new string value containing b.
    68  //
    69  // The returned string is valid until Reset is called on a.
    70  func (a *Arena) NewStringBytes(b []byte) *Value {
    71  	v := a.c.getValue()
    72  	v.t = typeRawString
    73  	bLen := len(a.b)
    74  	a.b = escapeString(a.b, b2s(b))
    75  	v.s = b2s(a.b[bLen+1 : len(a.b)-1])
    76  	return v
    77  }
    78  
    79  // NewNumberFloat64 returns new number value containing f.
    80  //
    81  // The returned number is valid until Reset is called on a.
    82  func (a *Arena) NewNumberFloat64(f float64) *Value {
    83  	v := a.c.getValue()
    84  	v.t = TypeNumber
    85  	bLen := len(a.b)
    86  	a.b = strconv.AppendFloat(a.b, f, 'g', -1, 64)
    87  	v.s = b2s(a.b[bLen:])
    88  	return v
    89  }
    90  
    91  // NewNumberInt returns new number value containing n.
    92  //
    93  // The returned number is valid until Reset is called on a.
    94  func (a *Arena) NewNumberInt(n int) *Value {
    95  	v := a.c.getValue()
    96  	v.t = TypeNumber
    97  	bLen := len(a.b)
    98  	a.b = strconv.AppendInt(a.b, int64(n), 10)
    99  	v.s = b2s(a.b[bLen:])
   100  	return v
   101  }
   102  
   103  // NewNumberString returns new number value containing s.
   104  //
   105  // The returned number is valid until Reset is called on a.
   106  func (a *Arena) NewNumberString(s string) *Value {
   107  	v := a.c.getValue()
   108  	v.t = TypeNumber
   109  	v.s = s
   110  	return v
   111  }
   112  
   113  // NewNull returns null value.
   114  func (a *Arena) NewNull() *Value {
   115  	return valueNull
   116  }
   117  
   118  // NewTrue returns true value.
   119  func (a *Arena) NewTrue() *Value {
   120  	return valueTrue
   121  }
   122  
   123  // NewFalse return false value.
   124  func (a *Arena) NewFalse() *Value {
   125  	return valueFalse
   126  }