-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringBuilder.php
More file actions
172 lines (154 loc) · 4.94 KB
/
StringBuilder.php
File metadata and controls
172 lines (154 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* A String Builder Class for PHP
*
* A String Class that makes appending strings much quicker
* then a straight append. Great for loops, etc.
*
* PHP version 5
*
* LICENSE: This software is MIT Licensed and there should be a copy
* of the license with this software.
*
* @category Utility
* @package StringBuilder
* @author Mike Curry <mikecurry74@gmail.com>
* @copyright 2015 Mike Curry
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
* @link https://github.com/mikecurry74/stringbuilder
*/
// Only define the class once
if (!class_exists('StringBuilder')) {
class StringBuilder {
// {{{ properties
/**
* The string
*
* Can contain any valid string.
*
* @var string
* @access private
*/
private $_string = '';
/**
* The actual string length of the stored string
*
* This returns the actual length, and not the entire length of
* the buffer string. If the string was "test" and the entire buffer
* length was 256, this would be set to 4.
*
* @var integer
* @access private
*/
private $_stringLength = 0;
/**
* The actual string length of the pre-defined buffer.
*
* This will return the pre-defined length of the string buffer.
* If the string surpasses the buffer length and is expanded, it will
* still return the original buffer length.
*
* @var integer
* @access private
*/
private $_bufferSize = 1024;
/**
* Class Constructor
*
* The StringBuilder constructor, all initialization happens here
*
* @param string $string Optional. The initial string to add on creation.
* @param integer bufferLength $bufferLength Optional. The amount of
* bytes to initially allocate to the string builder.
*/
public function __construct($string = '', $bufferLength = -1) {
// if not set, use the default
if ($bufferLength === -1) {
$bufferLength = $this->_bufferSize;
} else {
// store the default
$this->_bufferSize = $bufferLength;
}
// initialize the buffer
$this->_string = str_repeat(' ', $bufferLength);
// append an initial string?
if (isset($string[0])) {
$this->append($string);
}
}
/**
* Default buffer size
*
* The default buffer size to be allocated
*
* @return integer Buffer size in bytes.
*/
public function bufferSize() {
return $this->_bufferSize;
}
/**
* Current buffer size
*
* Returns the total bytes currently in use, includes unused buffer
* bytes as well.
*
* @return integer Current string Buffer size in bytes.
*/
public function getActualBufferSize() {
return strlen($this->_string);
}
/**
* Current string length
*
* Returns the total string length in bytes, doesn't included unused
* buffer spsace. Basically returns your string length.
*
* @return integer Current string length in bytes.
*/
public function length() {
return $this->_stringLength;
}
/**
* Appender function
*
* This is the function that appends the data to the string.
*
* @param string $string Required. The string to append
*/
public function append($string) {
$len = strlen($string);
$i = 0;
// do we need to allocate more space?
if (!isset($this->_string[$this->_stringLength + $len])) {
$this->_string .= str_repeat(' ', $this->_bufferSize);
}
// insert the string into the buffer
for ($i = 0; $i < $len; ++$i) {
$pos = $this->_stringLength + $i;
$this->_string[$pos] = $string[$i];
}
//echo $this->_string;
// update the string length
$this->_stringLength += $len;
}
/**
* ToString
*
* Returns the completed string.
*
* @return string String text.
*/
public function toString() {
if ($this->_stringLength) {
if (is_array($this->_string)) {
return substr(implode($this->_string), 0, $this->_stringLength);
} else {
return substr($this->_string, 0, $this->_stringLength);
}
} else {
return '';
}
}
}
}