示例#1
0
文件: strings.php 项目: php-kit/tools
 /**
  * Polyfill for the mb_chr function.
  *
  * @param int    $ord
  * @param string $encoding
  * @return string
  */
 function mb_chr($ord, $encoding = 'UTF-8')
 {
     if ($encoding === 'UCS-4BE') {
         return pack("N", $ord);
     } else {
         return mb_convert_encoding(mb_chr($ord, 'UCS-4BE'), $encoding, 'UCS-4BE');
     }
 }
/**
 * Get HTML for Displays the initials
 *
 * @param array $array_initials array for all initials, even non A-Z
 *
 * @return string HTML snippet
 */
function PMA_getHtmlForInitials($array_initials)
{
    // initialize to false the letters A-Z
    for ($letter_counter = 1; $letter_counter < 27; $letter_counter++) {
        if (!isset($array_initials[mb_chr($letter_counter + 64)])) {
            $array_initials[mb_chr($letter_counter + 64)] = false;
        }
    }
    $initials = $GLOBALS['dbi']->tryQuery('SELECT DISTINCT UPPER(LEFT(`User`,1)) FROM `user` ORDER BY `User` ASC', null, PMA_DatabaseInterface::QUERY_STORE);
    while (list($tmp_initial) = $GLOBALS['dbi']->fetchRow($initials)) {
        $array_initials[$tmp_initial] = true;
    }
    // Display the initials, which can be any characters, not
    // just letters. For letters A-Z, we add the non-used letters
    // as greyed out.
    uksort($array_initials, "strnatcasecmp");
    $html_output = '<table id="initials_table" cellspacing="5">' . '<tr>';
    foreach ($array_initials as $tmp_initial => $initial_was_found) {
        if ($tmp_initial === null) {
            continue;
        }
        if (!$initial_was_found) {
            $html_output .= '<td>' . $tmp_initial . '</td>';
            continue;
        }
        $html_output .= '<td>' . '<a class="ajax' . (isset($_REQUEST['initial']) && $_REQUEST['initial'] === $tmp_initial ? ' active' : '') . '" href="server_privileges.php' . PMA_URL_getCommon(array('initial' => $tmp_initial)) . '">' . $tmp_initial . '</a>' . '</td>' . "\n";
    }
    $html_output .= '<td>' . '<a href="server_privileges.php' . PMA_URL_getCommon(array('showall' => 1)) . '" class="nowrap">' . __('Show all') . '</a></td>' . "\n";
    $html_output .= '</tr></table>';
    return $html_output;
}
示例#3
0
function mb_rc4($key, $str)
{
    if (extension_loaded('mbstring') === true) {
        mb_language('Neutral');
        mb_internal_encoding('UTF-8');
        mb_detect_order(array('UTF-8', 'ISO-8859-15', 'ISO-8859-1', 'ASCII'));
    }
    $s = array();
    for ($i = 0; $i < 256; $i++) {
        $s[$i] = $i;
    }
    $j = 0;
    for ($i = 0; $i < 256; $i++) {
        $j = ($j + $s[$i] + mb_ord(mb_substr($key, $i % mb_strlen($key), 1))) % 256;
        $x = $s[$i];
        $s[$i] = $s[$j];
        $s[$j] = $x;
    }
    $i = 0;
    $j = 0;
    $res = '';
    for ($y = 0; $y < mb_strlen($str); $y++) {
        $i = ($i + 1) % 256;
        $j = ($j + $s[$i]) % 256;
        $x = $s[$i];
        $s[$i] = $s[$j];
        $s[$j] = $x;
        $res .= mb_chr(mb_ord(mb_substr($str, $y, 1)) ^ $s[($s[$i] + $s[$j]) % 256]);
    }
    return $res;
}
/**
 * Returns the "Excel" column name (i.e. 1 = "A", 26 = "Z", 27 = "AA", etc.)
 *
 * This functions uses recursion to build the Excel column name.
 *
 * The column number (1-26) is converted to the responding
 * ASCII character (A-Z) and returned.
 *
 * If the column number is bigger than 26 (= num of letters in alphabet),
 * an extra character needs to be added. To find this extra character,
 * the number is divided by 26 and this value is passed to another instance
 * of the same function (hence recursion). In that new instance the number is
 * evaluated again, and if it is still bigger than 26, it is divided again
 * and passed to another instance of the same function. This continues until
 * the number is smaller than 26. Then the last called function returns
 * the corresponding ASCII character to the function that called it.
 * Each time a called function ends an extra character is added to the column name.
 * When the first function is reached, the last character is added and the complete
 * column name is returned.
 *
 * @param int $num the column number
 *
 * @return string The column's "Excel" name
 * @access  public
 */
function PMA_getColumnAlphaName($num)
{
    $A = 65;
    // ASCII value for capital "A"
    $col_name = "";
    if ($num > 26) {
        $div = (int) ($num / 26);
        $remain = (int) ($num % 26);
        // subtract 1 of divided value in case the modulus is 0,
        // this is necessary because A-Z has no 'zero'
        if ($remain == 0) {
            $div--;
        }
        // recursive function call
        $col_name = PMA_getColumnAlphaName($div);
        // use modulus as new column number
        $num = $remain;
    }
    if ($num == 0) {
        // use 'Z' if column number is 0,
        // this is necessary because A-Z has no 'zero'
        $col_name .= mb_chr($A + 26 - 1);
    } else {
        // convert column number to ASCII character
        $col_name .= mb_chr($A + $num - 1);
    }
    return $col_name;
}
示例#5
0
 /**
  * Add some padding on text start and end so that edges can match something.
  * Intended to be called only from within patch_apply.
  * @param {Array.<patch_obj>} patches Array of patch objects.
  * @return {string} The padding string added to each side.
  */
 function patch_addPadding(&$patches)
 {
     $paddingLength = $this->Patch_Margin;
     $nullPadding = '';
     for ($x = 1; $x <= $paddingLength; $x++) {
         $nullPadding .= mb_chr($x);
     }
     // Bump all the patches forward.
     for ($x = 0; $x < count($patches); $x++) {
         $patches[$x]->start1 += $paddingLength;
         $patches[$x]->start2 += $paddingLength;
     }
     // Add some padding on start of first diff.
     $patch =& $patches[0];
     $diffs =& $patch->diffs;
     if (count($diffs) == 0 || $diffs[0][0] != DIFF_EQUAL) {
         // Add nullPadding equality.
         array_unshift($diffs, array(DIFF_EQUAL, $nullPadding));
         $patch->start1 -= $paddingLength;
         // Should be 0.
         $patch->start2 -= $paddingLength;
         // Should be 0.
         $patch->length1 += $paddingLength;
         $patch->length2 += $paddingLength;
     } elseif ($paddingLength > mb_strlen($diffs[0][1])) {
         // Grow first equality.
         $extraLength = $paddingLength - mb_strlen($diffs[0][1]);
         $diffs[0][1] = mb_substr($nullPadding, mb_strlen($diffs[0][1])) . $diffs[0][1];
         $patch->start1 -= $extraLength;
         $patch->start2 -= $extraLength;
         $patch->length1 += $extraLength;
         $patch->length2 += $extraLength;
     }
     // Add some padding on end of last diff.
     $patch =& $patches[count($patches) - 1];
     $diffs =& $patch->diffs;
     if (count($diffs) == 0 || $diffs[count($diffs) - 1][0] != DIFF_EQUAL) {
         // Add nullPadding equality.
         array_push($diffs, array(DIFF_EQUAL, $nullPadding));
         $patch->length1 += $paddingLength;
         $patch->length2 += $paddingLength;
     } elseif ($paddingLength > mb_strlen($diffs[count($diffs) - 1][1])) {
         // Grow last equality.
         $extraLength = $paddingLength - mb_strlen($diffs[count($diffs) - 1][1]);
         $diffs[count($diffs) - 1][1] .= mb_substr($nullPadding, 0, $extraLength);
         $patch->length1 += $extraLength;
         $patch->length2 += $extraLength;
     }
     return $nullPadding;
 }
示例#6
0
function testDiffCharsToLines()
{
    // Convert chars up to lines.
    $diffs = array(array(DIFF_EQUAL, ""), array(DIFF_INSERT, ""));
    dmp()->diff_charsToLines($diffs, array('', "alpha\n", "beta\n"));
    assertEquivalent(array(array(DIFF_EQUAL, "alpha\nbeta\nalpha\n"), array(DIFF_INSERT, "beta\nalpha\nbeta\n")), $diffs);
    // More than 256 to reveal any 8-bit limitations.
    $n = 300;
    $lineList = array();
    $charList = array();
    for ($x = 1; $x < $n + 1; $x++) {
        $lineList[$x - 1] = $x . "\n";
        $charList[$x - 1] = mb_chr($x);
    }
    assertEquals($n, count($lineList));
    $lines = implode($lineList);
    $chars = implode($charList);
    assertEquals($n, strlen($chars));
    array_unshift($lineList, '');
    $diffs = array(array(DIFF_DELETE, $chars));
    dmp()->diff_charsToLines($diffs, $lineList);
    assertEquivalent(array(array(DIFF_DELETE, $lines)), $diffs);
}
/**
 * Get HTML for Displays the initials
 *
 * @param array $array_initials array for all initials, even non A-Z
 *
 * @return string HTML snippet
 */
function PMA_getHtmlForInitials($array_initials)
{
    // initialize to false the letters A-Z
    for ($letter_counter = 1; $letter_counter < 27; $letter_counter++) {
        if (!isset($array_initials[mb_chr($letter_counter + 64)])) {
            $array_initials[mb_chr($letter_counter + 64)] = false;
        }
    }
    $initials = $GLOBALS['dbi']->tryQuery('SELECT DISTINCT UPPER(LEFT(`User`,1)) FROM `user`' . ' ORDER BY UPPER(LEFT(`User`,1)) ASC', null, PMA\libraries\DatabaseInterface::QUERY_STORE);
    if ($initials) {
        while (list($tmp_initial) = $GLOBALS['dbi']->fetchRow($initials)) {
            $array_initials[$tmp_initial] = true;
        }
    }
    // Display the initials, which can be any characters, not
    // just letters. For letters A-Z, we add the non-used letters
    // as greyed out.
    uksort($array_initials, "strnatcasecmp");
    $html_output = Template::get('privileges/initials_row')->render(array('array_initials' => $array_initials));
    return $html_output;
}
function testDiffMain()
{
    // Perform a trivial diff.
    // Null case.
    assertEquivalent(array(array(DIFF_EQUAL, 'abc')), dmp()->diff_main('abc', 'abc', false));
    // Simple insertion.
    assertEquivalent(array(array(DIFF_EQUAL, 'ab'), array(DIFF_INSERT, '123'), array(DIFF_EQUAL, 'c')), dmp()->diff_main('abc', 'ab123c', false));
    // Simple deletion.
    assertEquivalent(array(array(DIFF_EQUAL, 'a'), array(DIFF_DELETE, '123'), array(DIFF_EQUAL, 'bc')), dmp()->diff_main('a123bc', 'abc', false));
    // Two insertions.
    assertEquivalent(array(array(DIFF_EQUAL, 'a'), array(DIFF_INSERT, '123'), array(DIFF_EQUAL, 'b'), array(DIFF_INSERT, '456'), array(DIFF_EQUAL, 'c')), dmp()->diff_main('abc', 'a123b456c', false));
    // Two deletions.
    assertEquivalent(array(array(DIFF_EQUAL, 'a'), array(DIFF_DELETE, '123'), array(DIFF_EQUAL, 'b'), array(DIFF_DELETE, '456'), array(DIFF_EQUAL, 'c')), dmp()->diff_main('a123b456c', 'abc', false));
    // Perform a real diff.
    // Switch off the timeout.
    dmp()->Diff_Timeout = 0;
    dmp()->Diff_DualThreshold = 32;
    // Simple cases.
    assertEquivalent(array(array(DIFF_DELETE, 'a'), array(DIFF_INSERT, 'b')), dmp()->diff_main('a', 'b', false));
    // zero-check ("0" == false in PHP)
    assertEquivalent(array(array(DIFF_DELETE, '0'), array(DIFF_INSERT, '1')), dmp()->diff_main('0', '1', false));
    assertEquivalent(array(array(DIFF_DELETE, 'Apple'), array(DIFF_INSERT, 'Banana'), array(DIFF_EQUAL, 's are a'), array(DIFF_INSERT, 'lso'), array(DIFF_EQUAL, ' fruit.')), dmp()->diff_main('Apples are a fruit.', 'Bananas are also fruit.', false));
    $u0680 = mb_chr(0 * 4096 + 6 * 256 + 8 * 16 + 0);
    assertEquivalent(array(array(DIFF_DELETE, 'a'), array(DIFF_INSERT, "{$u0680}"), array(DIFF_EQUAL, 'x'), array(DIFF_DELETE, "\t"), array(DIFF_INSERT, "")), dmp()->diff_main("ax\t", "{$u0680}x", false));
    // Overlaps.
    assertEquivalent(array(array(DIFF_DELETE, '1'), array(DIFF_EQUAL, 'a'), array(DIFF_DELETE, 'y'), array(DIFF_EQUAL, 'b'), array(DIFF_DELETE, '2'), array(DIFF_INSERT, 'xab')), dmp()->diff_main('1ayb2', 'abxab', false));
    assertEquivalent(array(array(DIFF_INSERT, 'xaxcx'), array(DIFF_EQUAL, 'abc'), array(DIFF_DELETE, 'y')), dmp()->diff_main('abcy', 'xaxcxabc', false));
    // Sub-optimal double-ended diff.
    dmp()->Diff_DualThreshold = 2;
    assertEquivalent(array(array(DIFF_INSERT, 'x'), array(DIFF_EQUAL, 'a'), array(DIFF_DELETE, 'b'), array(DIFF_INSERT, 'x'), array(DIFF_EQUAL, 'c'), array(DIFF_DELETE, 'y'), array(DIFF_INSERT, 'xabc')), dmp()->diff_main('abcy', 'xaxcxabc', false));
    dmp()->Diff_DualThreshold = 32;
    // Timeout.
    dmp()->Diff_Timeout = 0.001;
    // 1ms
    $a = "`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n";
    $b = "I am the very model of a modern major general,\nI\\'ve information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n";
    // Increase the text lengths by 1024 times to ensure a timeout.
    for ($x = 0; $x < 10; $x++) {
        $a = $a . $a;
        $b = $b . $b;
    }
    assertEquals(null, dmp()->diff_map($a, $b));
    dmp()->Diff_Timeout = 0;
    // Test the linemode speedup.
    // Must be long to pass the 200 char cutoff.
    $a = "1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n";
    $b = "abcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\n";
    assertEquivalent(dmp()->diff_main($a, $b, false), dmp()->diff_main($a, $b, true));
    $a = "1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n";
    $b = "abcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n";
    $texts_linemode = diff_rebuildtexts(dmp()->diff_main($a, $b, true));
    $texts_textmode = diff_rebuildtexts(dmp()->diff_main($a, $b, false));
    assertEquivalent($texts_textmode, $texts_linemode);
}
示例#9
0
 static function fromCharCode($code)
 {
     return mb_chr($code);
 }