Skip to content

Commit

Permalink
Reduce amount of trimming on strings
Browse files Browse the repository at this point in the history
  • Loading branch information
flodolo committed Jan 11, 2017
1 parent e53e117 commit 6b715e2
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/classes/Transvision/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function __construct()
*/
public function setSearchTerms($string)
{
$this->search_terms = trim($string);
$this->search_terms = $string;
$this->regex_search_terms = $this->search_terms;
$this->updateRegex();

Expand Down
8 changes: 4 additions & 4 deletions app/classes/Transvision/ShowResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static function getRepositorySearchResults($entities, $array_strings)
$output = [];

$clean_string = function ($string) {
return trim(htmlspecialchars_decode($string, ENT_QUOTES));
return htmlspecialchars_decode($string, ENT_QUOTES);
};

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

$component = explode('/', $key)[0];
$fileAndRawString = explode(':', $key);
$source_string = trim($strings[0]);
$target_string = trim($strings[1]);
$source_string = $strings[0];
$target_string = $strings[1];

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

if ($extra_locale) {
$target_string2 = trim($strings[2]);
$target_string2 = $strings[2];
$entity_link = "?sourcelocale={$locale1}"
. "&locale={$search_object->getLocale('extra')}"
. "&repo={$current_repo}"
Expand Down
10 changes: 4 additions & 6 deletions app/classes/Transvision/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@
class Strings
{
/**
* Replace contiguous spaces in a string by a single space
* Replace contiguous spaces in a string by a single space.
* Leading and trailing spaces are preserved but collapsed
* to a single space.
*
* @param string $string The string to analyze
* @return string Cleaned up string with extra spaces merged
*/
public static function mtrim($string)
{
$string = explode(' ', $string);
$string = array_filter($string);
$string = implode(' ', $string);

return $string;
return preg_replace('/\s+/', ' ', $string);
}

/**
Expand Down
3 changes: 0 additions & 3 deletions app/classes/Transvision/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ public static function cleanString($string)
return '';
}

// Filter out double spaces
$string = Strings::mtrim($string);

return $string;
}

Expand Down
4 changes: 2 additions & 2 deletions app/models/api/entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
$strings = Utils::getRepoStrings($locale_code, $repo);

if (isset($strings[$entity])) {
$strings[$entity] = trim($strings[$entity]);
$strings[$entity] = $strings[$entity];
if (Strings::endsWith(strtolower($strings[$entity]), '{ok}')) {
$strings[$entity] = trim(substr($strings[$entity], 0, -4));
$strings[$entity] = substr($strings[$entity], 0, -4);
}
$translations[$locale_code] = $strings[$entity];
}
Expand Down
4 changes: 2 additions & 2 deletions tests/units/Transvision/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public function testSetSearchTerms()
$obj->setSearchTerms(' foobar ');
$this
->string($obj->getSearchTerms())
->isEqualTo('foobar');
->isEqualTo(' foobar ');
$this
->string($obj->getRegexSearchTerms())
->isEqualTo('foobar');
->isEqualTo(' foobar ');
}

public function testSetRegexSearchTerms()
Expand Down
11 changes: 8 additions & 3 deletions tests/units/Transvision/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@ class Strings extends atoum\test
{
public function mtrimDP()
{
return ['Le cheval blanc '];
return [
['Le cheval blanc ', 'Le cheval blanc '],
[' Le cheval blanc', ' Le cheval blanc'],
[' Le cheval blanc ', ' Le cheval blanc '],
['Le cheval blanc', 'Le cheval blanc'],
];
}

/**
* @dataProvider mtrimDP
*/
public function testMtrim($a)
public function testMtrim($a, $b)
{
$obj = new _Strings();
$this
->string($obj->mtrim($a))
->isEqualTo('Le cheval blanc');
->isEqualTo($b);
}

public function startsWithDP()
Expand Down
8 changes: 2 additions & 6 deletions tests/units/Transvision/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,8 @@ public function cleanStringDP()
{
return [
[
'pages web fréquemment visitées (en cliquant sur « Recharger »,',
'pages web fréquemment visitées (en cliquant sur « Recharger »,',
],
[
'toto ',
'toto',
[],
'',
],
[
"don\u0027t strip unicode escaped chars",
Expand Down
1 change: 1 addition & 0 deletions web/style/transvision.css
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ table tr td.no_highligth {
color: #000;
background-color: rgba(255, 165, 0, 0.77);
border-radius: 3px;
white-space: pre-wrap;
}

.superset {
Expand Down

0 comments on commit 6b715e2

Please sign in to comment.