public function testStripTail() { $this->assertEquals('string with cont', \r8\str\stripTail("string with content", "ent")); $this->assertEquals('string with cont', \r8\str\stripTail("string with cont", "ent")); $this->assertEquals('string with cont', \r8\str\stripTail("string with content", "ent", TRUE)); $this->assertEquals('string with cont', \r8\str\stripTail("string with content", "ENT", TRUE)); $this->assertEquals('string with cont', \r8\str\stripTail("string with cont", "ent", TRUE)); $this->assertEquals('string with cont', \r8\str\stripTail("string with content", "ent", FALSE)); $this->assertEquals('string with content', \r8\str\stripTail("string with content", "Ent", FALSE)); $this->assertEquals('string with cont', \r8\str\stripTail("string with cont", "ent", FALSE)); $this->assertEquals('', \r8\str\stripTail("string", "string", TRUE)); $this->assertEquals('string', \r8\str\stripTail("string", "")); }
/** * Decodes an encoded string * * @param mixed $value The value to decode * @return mixed The original, unencoded value */ public function from($string) { $string = trim((string) $string); $string = \r8\str\stripHead($string, "<~"); $string = \r8\str\stripTail($string, "~>"); $length = strlen($string); $result = ""; $tuple = 0; $j = 0; // Loop over each character in the input for ($i = 0; $i < $length; $i++) { $chr = $string[$i]; if ($j != 0 && ($chr == "z" || $chr == "y")) { $err = new \r8\Exception\Data($chr, "Unexpected Character", "Misplaced compression character"); $err->addData("Encoded String", $string); throw $err; } switch ($chr) { // Add this character to the tuple default: // Grab the character code and ensure it is a valid character $ord = ord($chr) - 33; if ($ord < 0 || $ord > 84) { $err = new \r8\Exception\Data($chr, "Invalid Character", "Invalid encoding character"); $err->addData("Encoded String", $string); throw $err; } // Integrate this character into the tuple $tuple += $ord * pow(85, 4 - $j); // If this isn't the last character in the tuple, move on if ($j < 4 && $i != $length - 1) { $j++; } else { // Compensate for an incomplete trailing tuple if ($j < 4) { for ($k = $j; $k <= 3; $k++) { $tuple += 85 * pow(85, 3 - $k); } } // Convert the 32bit integer to binary form $tuple = str_pad(decbin($tuple), 32, "0", STR_PAD_LEFT); // Split the binary into 8 bit segments and convert each back to an integer $tuple = array_map("bindec", str_split($tuple, 8)); if ($j < 4) { $tuple = array_slice($tuple, 0, $j); } // Convert each int into a character, then combine them $result .= implode("", array_map("chr", $tuple)); // Reset the tuple to prepare for the next substring $tuple = 0; $j = 0; } break; // Handle z compression // Handle z compression case "z": $result .= chr(0) . chr(0) . chr(0) . chr(0); break; // Handle y compression // Handle y compression case "y": $result .= " "; break; // Skip over white space // Skip over white space case "\n": case "\r": case "\t": case " ": case "": case "\f": case "": break; } } return $result; }
/** * Combines two strings and adds a separator between them. * * Detects if the separator already exists at the tail or head of either string * so that it isn't repeated. * * @param String $string1 The first part of the resulting string * @param String $string2 The last part of the resulting string * @param String $glue The glue to put between the two strings * @param Boolean $ignoreCase Whether the comparison should be case sensitive * @return String Returns the strings combined, with the glue in the middle */ function weld($string1, $string2, $glue, $ignoreCase = TRUE) { $string1 = (string) $string1; $string2 = (string) $string2; $glue = (string) $glue; if (\r8\isVague($glue, \r8\str\ALLOW_SPACES)) { return $string1 . $string2; } return \r8\str\stripTail($string1, $glue, $ignoreCase) . $glue . \r8\str\stripHead($string2, $glue, $ignoreCase); }