Skip to content

Commit 6b715e2

Browse files
committed
Reduce amount of trimming on strings
1 parent e53e117 commit 6b715e2

9 files changed

Lines changed: 24 additions & 27 deletions

File tree

app/classes/Transvision/Search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function __construct()
132132
*/
133133
public function setSearchTerms($string)
134134
{
135-
$this->search_terms = trim($string);
135+
$this->search_terms = $string;
136136
$this->regex_search_terms = $this->search_terms;
137137
$this->updateRegex();
138138

app/classes/Transvision/ShowResults.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public static function getRepositorySearchResults($entities, $array_strings)
173173
$output = [];
174174

175175
$clean_string = function ($string) {
176-
return trim(htmlspecialchars_decode($string, ENT_QUOTES));
176+
return htmlspecialchars_decode($string, ENT_QUOTES);
177177
};
178178

179179
foreach ($search_results as $entity => $set) {
@@ -289,8 +289,8 @@ public static function resultsTable($search_object, $search_results, $page)
289289

290290
$component = explode('/', $key)[0];
291291
$fileAndRawString = explode(':', $key);
292-
$source_string = trim($strings[0]);
293-
$target_string = trim($strings[1]);
292+
$source_string = $strings[0];
293+
$target_string = $strings[1];
294294

295295
$entity_link = "?sourcelocale={$locale1}"
296296
. "&locale={$locale2}"
@@ -303,7 +303,7 @@ public static function resultsTable($search_object, $search_results, $page)
303303
)];
304304

305305
if ($extra_locale) {
306-
$target_string2 = trim($strings[2]);
306+
$target_string2 = $strings[2];
307307
$entity_link = "?sourcelocale={$locale1}"
308308
. "&locale={$search_object->getLocale('extra')}"
309309
. "&repo={$current_repo}"

app/classes/Transvision/Strings.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@
1111
class Strings
1212
{
1313
/**
14-
* Replace contiguous spaces in a string by a single space
14+
* Replace contiguous spaces in a string by a single space.
15+
* Leading and trailing spaces are preserved but collapsed
16+
* to a single space.
1517
*
1618
* @param string $string The string to analyze
1719
* @return string Cleaned up string with extra spaces merged
1820
*/
1921
public static function mtrim($string)
2022
{
21-
$string = explode(' ', $string);
22-
$string = array_filter($string);
23-
$string = implode(' ', $string);
24-
25-
return $string;
23+
return preg_replace('/\s+/', ' ', $string);
2624
}
2725

2826
/**

app/classes/Transvision/Utils.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,6 @@ public static function cleanString($string)
233233
return '';
234234
}
235235

236-
// Filter out double spaces
237-
$string = Strings::mtrim($string);
238-
239236
return $string;
240237
}
241238

app/models/api/entity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
$strings = Utils::getRepoStrings($locale_code, $repo);
1313

1414
if (isset($strings[$entity])) {
15-
$strings[$entity] = trim($strings[$entity]);
15+
$strings[$entity] = $strings[$entity];
1616
if (Strings::endsWith(strtolower($strings[$entity]), '{ok}')) {
17-
$strings[$entity] = trim(substr($strings[$entity], 0, -4));
17+
$strings[$entity] = substr($strings[$entity], 0, -4);
1818
}
1919
$translations[$locale_code] = $strings[$entity];
2020
}

tests/units/Transvision/Search.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ public function testSetSearchTerms()
6060
$obj->setSearchTerms(' foobar ');
6161
$this
6262
->string($obj->getSearchTerms())
63-
->isEqualTo('foobar');
63+
->isEqualTo(' foobar ');
6464
$this
6565
->string($obj->getRegexSearchTerms())
66-
->isEqualTo('foobar');
66+
->isEqualTo(' foobar ');
6767
}
6868

6969
public function testSetRegexSearchTerms()

tests/units/Transvision/Strings.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@ class Strings extends atoum\test
1010
{
1111
public function mtrimDP()
1212
{
13-
return ['Le cheval blanc '];
13+
return [
14+
['Le cheval blanc ', 'Le cheval blanc '],
15+
[' Le cheval blanc', ' Le cheval blanc'],
16+
[' Le cheval blanc ', ' Le cheval blanc '],
17+
['Le cheval blanc', 'Le cheval blanc'],
18+
];
1419
}
1520

1621
/**
1722
* @dataProvider mtrimDP
1823
*/
19-
public function testMtrim($a)
24+
public function testMtrim($a, $b)
2025
{
2126
$obj = new _Strings();
2227
$this
2328
->string($obj->mtrim($a))
24-
->isEqualTo('Le cheval blanc');
29+
->isEqualTo($b);
2530
}
2631

2732
public function startsWithDP()

tests/units/Transvision/Utils.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,8 @@ public function cleanStringDP()
178178
{
179179
return [
180180
[
181-
'pages web fréquemment visitées (en cliquant sur « Recharger »,',
182-
'pages web fréquemment visitées (en cliquant sur « Recharger »,',
183-
],
184-
[
185-
'toto ',
186-
'toto',
181+
[],
182+
'',
187183
],
188184
[
189185
"don\u0027t strip unicode escaped chars",

web/style/transvision.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ table tr td.no_highligth {
512512
color: #000;
513513
background-color: rgba(255, 165, 0, 0.77);
514514
border-radius: 3px;
515+
white-space: pre-wrap;
515516
}
516517

517518
.superset {

0 commit comments

Comments
 (0)