Changeset View
Changeset View
Standalone View
Standalone View
src/utils/utils.php
Show First 20 Lines • Show All 894 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Split a corpus of text into lines. This function splits on "\n", "\r\n", or | * Split a corpus of text into lines. This function splits on "\n", "\r\n", or | ||||
* a mixture of any of them. | * a mixture of any of them. | ||||
* | * | ||||
* NOTE: This function does not treat "\r" on its own as a newline because none | * NOTE: This function does not treat "\r" on its own as a newline because none | ||||
* of SVN, Git or Mercurial do on any OS. | * of SVN, Git or Mercurial do on any OS. | ||||
* | * | ||||
* @param string Block of text to be split into lines. | * @param string|PhutilSafeHTML $corpus Block of text to be split into lines. | ||||
valerio.bozzolan: The already existing PHPDoc was wrong.
The `$corpus` parameter is not necessarily a string but… | |||||
* @param bool If true, retain line endings in result strings. | * @param bool If true, retain line endings in result strings. | ||||
* @return list List of lines. | * @return list List of lines. | ||||
* | * | ||||
* @phutil-external-symbol class PhutilSafeHTML | * @phutil-external-symbol class PhutilSafeHTML | ||||
* @phutil-external-symbol function phutil_safe_html | * @phutil-external-symbol function phutil_safe_html | ||||
*/ | */ | ||||
function phutil_split_lines($corpus, $retain_endings = true) { | function phutil_split_lines($corpus, $retain_endings = true) { | ||||
if (!strlen($corpus)) { | if (!phutil_nonempty_stringlike($corpus)) { | ||||
Not Done Inline ActionsSadly it seems using phutil_nonempty_string() in this case can crash since $corpus can be an object. We probably need the phutil_nonempty_stringlike() trick instead. valerio.bozzolan: Sadly it seems using `phutil_nonempty_string()` in this case can crash since `$corpus` can be… | |||||
return array(''); | return array(''); | ||||
} | } | ||||
// Split on "\r\n" or "\n". | // Split on "\r\n" or "\n". | ||||
if ($retain_endings) { | if ($retain_endings) { | ||||
$lines = preg_split('/(?<=\n)/', $corpus); | $lines = preg_split('/(?<=\n)/', $corpus); | ||||
} else { | } else { | ||||
$lines = preg_split('/\r?\n/', $corpus); | $lines = preg_split('/\r?\n/', $corpus); | ||||
} | } | ||||
// If the text ends with "\n" or similar, we'll end up with an empty string | // If the text ends with "\n" or similar, we'll end up with an empty string | ||||
// at the end; discard it. | // at the end; discard it. | ||||
if (end($lines) == '') { | if (end($lines) == '') { | ||||
array_pop($lines); | array_pop($lines); | ||||
} | } | ||||
if ($corpus instanceof PhutilSafeHTML) { | if ($corpus instanceof PhutilSafeHTML) { | ||||
Done Inline ActionsInterestingly this line highlights that $corpus is not always a string valerio.bozzolan: Interestingly this line highlights that `$corpus` is not always a string | |||||
foreach ($lines as $key => $line) { | foreach ($lines as $key => $line) { | ||||
$lines[$key] = phutil_safe_html($line); | $lines[$key] = phutil_safe_html($line); | ||||
} | } | ||||
return $lines; | return $lines; | ||||
} | } | ||||
return $lines; | return $lines; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 1,288 Lines • Show Last 20 Lines |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0
The already existing PHPDoc was wrong.
The $corpus parameter is not necessarily a string but can be a PhutilSafeHTML as can be see at line 928