Skip to content

Commit 9eaa0c9

Browse files
author
Martin Brecht-Precht
committed
WIP: Related to issue #1
1 parent 186ef4c commit 9eaa0c9

File tree

1 file changed

+172
-29
lines changed

1 file changed

+172
-29
lines changed

src/SimpleStringBuilder.php

+172-29
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,41 @@ class SimpleStringBuilder
1313
{
1414

1515
/**
16-
* @var array
16+
* @var string
1717
*/
18-
private $strings = array();
18+
private $string;
19+
20+
/**
21+
* SimpleStringBuilder constructor.
22+
*
23+
* @param null $string
24+
*/
25+
public function __construct($string = null)
26+
{
27+
if (!is_null($string)) {
28+
if (!is_scalar($string)) {
29+
$type = is_object($string) ? get_class($string) : gettype($string);
30+
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
31+
}
32+
$this->string = (string)$string;
33+
}
34+
}
35+
36+
/**
37+
* @param int $position
38+
* @return string
39+
*/
40+
public function charAt($position)
41+
{
42+
if (!is_int($position)) {
43+
$type = is_object($position) ? get_class($position) : gettype($position);
44+
throw new \InvalidArgumentException('Position invalid. Expected integer. Got ' . $type . '.');
45+
}
46+
if ($position > $this->length()) {
47+
return null;
48+
}
49+
return mb_substr($this->string, $position, 1);
50+
}
1951

2052
/**
2153
* @param string $string
@@ -27,7 +59,7 @@ public function append($string)
2759
$type = is_object($string) ? get_class($string) : gettype($string);
2860
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
2961
}
30-
$this->strings[] = (string)$string;
62+
$this->string .= (string)$string;
3163
return $this;
3264
}
3365

@@ -41,7 +73,7 @@ public function prepend($string)
4173
$type = is_object($string) ? get_class($string) : gettype($string);
4274
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
4375
}
44-
array_unshift($this->strings, (string)$string);
76+
$this->string = $this->string . (string)$string;
4577
return $this;
4678
}
4779

@@ -50,29 +82,118 @@ public function prepend($string)
5082
* @param string $string
5183
* @return $this
5284
*/
53-
public function replace($position, $string)
85+
public function insert($position, $string)
5486
{
87+
if (!is_int($position)) {
88+
$type = is_object($position) ? get_class($position) : gettype($position);
89+
throw new \InvalidArgumentException('Position invalid. Expected integer. Got ' . $type . '.');
90+
}
5591
if (!is_scalar($string)) {
5692
$type = is_object($string) ? get_class($string) : gettype($string);
5793
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
5894
}
59-
if (!isset($this->strings[$position])) {
60-
throw new \InvalidArgumentException('Position ' . (string)$position . ' invalid.');
95+
if ($position > $this->length()) {
96+
$position = $this->length();
6197
}
62-
array_splice($this->strings, $position, 1, (string)$string);
98+
$this->string = mb_substr($this->string, 0, $position) . (string)$string . mb_substr($this->string, $position);
6399
return $this;
64100
}
65101

66102
/**
67103
* @param int $position
104+
* @param int $length
105+
* @param string $string
68106
* @return $this
69107
*/
70-
public function remove($position)
108+
public function replace($position, $length, $string)
71109
{
72-
if (!isset($this->strings[$position])) {
73-
throw new \InvalidArgumentException('Position ' . (string)$position . ' invalid.');
110+
if (!is_int($position)) {
111+
$type = is_object($position) ? get_class($position) : gettype($position);
112+
throw new \InvalidArgumentException('Position invalid. Expected integer. Got ' . $type . '.');
113+
}
114+
if (!is_int($length)) {
115+
$type = is_object($length) ? get_class($length) : gettype($length);
116+
throw new \InvalidArgumentException('Length invalid. Expected integer. Got ' . $type . '.');
74117
}
75-
array_splice($this->strings, $position, 1);
118+
if (!is_scalar($string)) {
119+
$type = is_object($string) ? get_class($string) : gettype($string);
120+
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
121+
}
122+
if ($position > $this->length()) {
123+
$position = $this->length();
124+
}
125+
$this->string = mb_substr($this->string, 0, $position) . (string)$string . mb_substr($this->string, $position + $length);
126+
return $this;
127+
}
128+
129+
/**
130+
* @param int $position
131+
* @param string $string
132+
* @return $this
133+
*/
134+
public function setCharAt($position, $string)
135+
{
136+
if (!is_int($position)) {
137+
$type = is_object($position) ? get_class($position) : gettype($position);
138+
throw new \InvalidArgumentException('Position invalid. Expected integer. Got ' . $type . '.');
139+
}
140+
if (!is_scalar($string)) {
141+
$type = is_object($string) ? get_class($string) : gettype($string);
142+
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
143+
}
144+
$this->string = mb_substr($this->string, 0, $position) . (string)$string . mb_substr($this->string, $position + 1);
145+
return $this;
146+
}
147+
148+
/**
149+
* @return $this
150+
*/
151+
public function reverse()
152+
{
153+
$this->string = strrev($this->string);
154+
return $this;
155+
}
156+
157+
/**
158+
* @param int $position
159+
* @param int $length
160+
* @return $this
161+
*/
162+
public function delete($position, $length = null)
163+
{
164+
if (!is_int($position)) {
165+
$type = is_object($position) ? get_class($position) : gettype($position);
166+
throw new \InvalidArgumentException('Position invalid. Expected integer. Got ' . $type . '.');
167+
}
168+
if (!is_int($length) && !is_null($length)) {
169+
$type = is_object($length) ? get_class($length) : gettype($length);
170+
throw new \InvalidArgumentException('Length invalid. Expected integer. Got ' . $type . '.');
171+
}
172+
if ($position > $this->length()) {
173+
return $this;
174+
}
175+
if (is_null($length)) {
176+
$this->string = mb_substr($this->string, 0, $position);
177+
} else {
178+
$this->string = mb_substr($this->string, 0, $position) . mb_substr($this->string, $position + $length);
179+
}
180+
return $this;
181+
}
182+
183+
/**
184+
* @param int $position
185+
* @return $this
186+
*/
187+
public function deleteCharAt($position)
188+
{
189+
if (!is_int($position)) {
190+
$type = is_object($position) ? get_class($position) : gettype($position);
191+
throw new \InvalidArgumentException('Position invalid. Expected integer. Got ' . $type . '.');
192+
}
193+
if ($position > $this->length()) {
194+
return $this;
195+
}
196+
$this->string = mb_substr($this->string, 0, $position) . mb_substr($this->string, $position + 1);
76197
return $this;
77198
}
78199

@@ -86,66 +207,88 @@ public function contains($string)
86207
$type = is_object($string) ? get_class($string) : gettype($string);
87208
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
88209
}
89-
for ($i = 0, $n = $this->size(); $i < $n; $i++) {
90-
if ($this->strings[$i] === (string)$string) {
91-
return true;
92-
}
210+
return strpos($this->string, (string)$string) !== false;
211+
}
212+
213+
/**
214+
* @param string $string
215+
* @param int $offset
216+
* @return int
217+
*/
218+
public function indexOf($string, $offset = 0)
219+
{
220+
if (!is_scalar($string)) {
221+
$type = is_object($string) ? get_class($string) : gettype($string);
222+
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
93223
}
94-
return false;
224+
if (!is_int($offset)) {
225+
$type = is_object($offset) ? get_class($offset) : gettype($offset);
226+
throw new \InvalidArgumentException('Offset invalid. Expected integer. Got ' . $type . '.');
227+
}
228+
return strpos($this->string, (string)$string, $offset);
95229
}
96230

97231
/**
98232
* @param string $string
99-
* @return bool
233+
* @param int $offset
234+
* @return int
100235
*/
101-
public function stringContains($string)
236+
public function lastIndexOf($string, $offset = 0)
102237
{
103238
if (!is_scalar($string)) {
104239
$type = is_object($string) ? get_class($string) : gettype($string);
105240
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
106241
}
107-
return strpos($this->build(), (string)$string) !== false;
242+
if (!is_int($offset)) {
243+
$type = is_object($offset) ? get_class($offset) : gettype($offset);
244+
throw new \InvalidArgumentException('Offset invalid. Expected integer. Got ' . $type . '.');
245+
}
246+
return strrpos($this->string, (string)$string, $offset);
108247
}
109248

110249
/**
111250
* @return int
112251
*/
113252
public function size()
114253
{
115-
return count($this->strings);
254+
return strlen($this->string);
116255
}
117256

118257
/**
119258
* @return int
120259
*/
121260
public function length()
122261
{
123-
return mb_strlen($this->build());
262+
return mb_strlen($this->string);
124263
}
125264

126265
/**
127266
* @param int $startPosition
128-
* @param int $size
267+
* @param int $length
129268
* @return string
130269
*/
131-
public function buildSubstring($startPosition, $size = null)
270+
public function buildSubstring($startPosition, $length = null)
132271
{
133-
if (!isset($this->strings[$startPosition])) {
272+
if ($this->length() > $startPosition) {
134273
throw new \InvalidArgumentException('Start position ' . (string)$startPosition . ' invalid.');
135274
}
136-
if (!is_int($size) && !is_null($size)) {
137-
$type = is_object($size) ? get_class($size) : gettype($size);
275+
if (!is_int($length) && !is_null($length)) {
276+
$type = is_object($length) ? get_class($length) : gettype($length);
138277
throw new \InvalidArgumentException('Length invalid. Expected integer. Got ' . $type . '.');
139278
}
140-
return implode('', array_slice($this->strings, $startPosition, $size));
279+
if (is_null($length)) {
280+
return mb_substr($this->string, $startPosition);
281+
} else {
282+
return mb_substr($this->string, $startPosition, $length);
283+
}
141284
}
142285

143286
/**
144287
* @return string
145288
*/
146289
public function build()
147290
{
148-
return implode('', $this->strings);
291+
return $this->string;
149292
}
150293

151294
}

0 commit comments

Comments
 (0)