~zorba-coders/zorba/feature-function-caching

« back to all changes in this revision

Viewing changes to src/util/string_util.h

  • Committer: Zorba Jenkins
  • Author(s): Paul J. Lucas
  • Date: 2014-03-06 19:36:03 UTC
  • mfrom: (10944.3.56 pjl-misc)
  • Revision ID: jenkins@lambda.nu-20140306193603-k3wu3y0qnwntmtpv
1. In a couple of cases, now reusing the same zstring so the cost of growing the string is not paid per value.
2. Added a string_appender class that "chunks" the appending of single characters onto the end of strings to reduce the number of times zstring::append() is called.
3. Improved creating of JSON objects by using iterators rather than operator[].
Approved: Matthias Brantner, Paul J. Lucas

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
 
51
51
using internal::ztd::c_str;
52
52
 
53
 
////////// String building /////////////////////////////////////////////////////
 
53
////////// String appending ///////////////////////////////////////////////////
 
54
 
 
55
/**
 
56
 * A %string_appender is used to optimize repeatedly appending characters to a
 
57
 * string in a loop by gathering \em N characters and appending them in chunks
 
58
 * so as to call the string's \c append() function less.
 
59
 */
 
60
template<class StringType,int BufCapacity>
 
61
class string_appender {
 
62
public:
 
63
  typedef StringType value_type;
 
64
  typedef typename value_type::value_type char_type;
 
65
  typedef typename value_type::size_type size_type;
 
66
 
 
67
  /**
 
68
   * Constructs an appender.
 
69
   *
 
70
   * @param s A pointer to the string to append to.
 
71
   */
 
72
  string_appender( value_type *s ) : s_( s ), buf_size_( 0 ) { }
 
73
 
 
74
  /**
 
75
   * Destroys the appender and appends any unappended characters to the string.
 
76
   */
 
77
  ~string_appender() {
 
78
    flush();
 
79
  }
 
80
 
 
81
  /**
 
82
   * Appends a character.
 
83
   *
 
84
   * @param c The character to append.
 
85
   * @return Returns \c *this.
 
86
   */
 
87
  string_appender& append( char_type c ) {
 
88
    buf_[ buf_size_++ ] = c;
 
89
    if ( buf_size_ == BufCapacity )
 
90
      flush();
 
91
    return *this;
 
92
  }
 
93
 
 
94
  /**
 
95
   * Appends any unappended characters to the string.
 
96
   */
 
97
  void flush() {
 
98
    s_->append( buf_, buf_size_ );
 
99
    buf_size_ = 0;
 
100
  }
 
101
 
 
102
  /**
 
103
   * Gets the string that is being appended to.
 
104
   *
 
105
   * @return Returns said string.
 
106
   */
 
107
  value_type& str() const {
 
108
    return *s_;
 
109
  }
 
110
 
 
111
  /**
 
112
   * Appends a character.
 
113
   *
 
114
   * @param c The character to append.
 
115
   * @return Returns \c *this.
 
116
   */
 
117
  string_appender& operator+=( char_type c ) {
 
118
    return append( c );
 
119
  }
 
120
 
 
121
private:
 
122
  char_type buf_[ BufCapacity ];
 
123
  size_type buf_size_;
 
124
  value_type *s_;
 
125
};
 
126
 
 
127
////////// String building ////////////////////////////////////////////////////
54
128
 
55
129
/**
56
130
 * A %string_builder is used to build (concatenate) strings on-the-fly and pass