Compares strings in constant time.

@param string $known_string
The expected string.
@param string $user_string
The user supplied string to check.

@return bool
Returns TRUE when the two strings are equal, FALSE otherwise.
161163
public static function hashEquals($known_string, $user_string) {
    if (function_exists('hash_equals')) {
      return hash_equals($known_string, $user_string);
    }
    else {
      // Backport of hash_equals() function from PHP 5.6
      // @see https://github.com/php/php-src/blob/PHP-5.6/ext/hash/hash.c#L739
      if (!is_string($known_string)) {
        trigger_error(sprintf("Expected known_string to be a string, %s given", gettype($known_string)), E_USER_WARNING);
        return FALSE;
      }

      if (!is_string($user_string)) {
        trigger_error(sprintf("Expected user_string to be a string, %s given", gettype($user_string)), E_USER_WARNING);
        return FALSE;
      }

      $known_len = strlen($known_string);
      if ($known_len !== strlen($user_string)) {
        return FALSE;
      }

      // This is security sensitive code. Do not optimize this for speed.
      $result = 0;
      for ($i = 0; $i < $known_len; $i++) {
        $result |= (ord($known_string[$i]) ^ ord($user_string[$i]));
      }

      return $result === 0;
    }
  }
1001493
public static function assertEqualsRegExp($compare, $string)
    {
        if (is_array($compare)) {
            $compare = implode('{{ \s* }}', $compare);
            $compare = str_replace(array('{{ \s* }}{{ ', ' }}{{ \s* }}'), array('{{ ', ' }}'), $compare);
        }
        if (empty($compare)) {
            return self::assertEquals($compare, $string);
        }
        $string = trim(str_replace(array("\r", "\n", "\t"), '', $string));
        $error = $string;
        preg_match_all('/{{\s[^\}]*\s}}/', $compare, $regexp);
        $equals = preg_split('/{{\s[^\}]*\s}}/', $compare);
        foreach ($equals as $key => $value) {
            if (!empty($value)) {
                try {
                    self::assertEquals($value, substr($string, 0, strlen($value)));
                } catch (\Exception $e) {
                    self::fail(implode("\n", array(
                        $e->getMessage(),
                        '--- Expected',
                        '+++ Actual',
                        '@@ @@',
                        self::merge(array_slice($equals, 0, $key, true), $regexp[0]),
                        '',
                        "-'".$value."'",
                        "+'".$string."'",
                    )));
                }
                $string = substr($string, strlen($value));
            }
            $next = (isset($equals[$key + 1])) ? $equals[$key + 1] : '';
            $length = (!empty($next)) ? strpos($string, $next) : 0;
            if (isset($regexp[0][$key]) && $length !== false) {
                $pattern = substr($regexp[0][$key], 3, -3);
                try {
                    self::assertRegExp('/^'.$pattern.'$/', substr($string, 0, $length));
                } catch (\Exception $e) {
                    if ('{{ '.$pattern.' }}' == substr($string, 0, $length)) {
                        // a literal (string) match
                    } else {
                        self::fail(implode("\n", array(
                            self::merge(array_slice($equals, 0, $key, true), $regexp[0]),
                            '',
                            $e->getMessage(),
                            $value.$regexp[0][$key].$next,
                            '',
                            self::merge(array_slice($equals, $key + 2, null, true), $regexp[0]),
                        )));
                    }
                }
                $string = substr($string, $length);
            }
        }
        if (!empty($string)) {
            return self::assertEquals(strstr($error, $string, true), $error);
        }
    }
1269693
private function compareBcStrings(string $a, string $b) : int
    {
        $a_len = strlen($a);
        $b_len = strlen($b);

        if ($a_len > $b_len) {
            return -1;
        }

        if ($a_len < $b_len) {
            return 1;
        }

        $a_len = strlen(str_replace(['*', '?'], '', $a));
        $b_len = strlen(str_replace(['*', '?'], '', $b));

        if ($a_len > $b_len) {
            return -1;
        }

        if ($a_len < $b_len) {
            return 1;
        }

        return 0;
    }
524472
public function equalsIgnoreCase($string): bool
    {
        try {
            Assert::typeOf(['string', __CLASS__], $string);
        } catch (InvalidArgumentException|TypeError $t) {
            return false;
        }

        return strcmp(
            mb_strtolower($this->data, $this->encoding),
            mb_strtolower((string) $string, $this->encoding)
        ) === 0;
    }
580645
public static function timingSafeEquals($safe, $user)
	{
		// Use builtin has comparison function available when running on PHP 5.6+
		if(function_exists('hash_equals'))
		{
			return hash_equals((string)$safe, (string)$user);
		}
		
		$safe .= chr(0);
		$user .= chr(0);

		$safeLen = strlen($safe);
		$userLen = strlen($user);
		$result = $safeLen - $userLen;

		for($i = 0; $i < $userLen; $i++)
		{
			$result |= (ord($safe[$i % $safeLen]) ^ ord($user[$i]));
		}

		return $result === 0;
	}
47682
def similarity(string_a, string_b)
      string_a, string_b = prep_strings(string_a, string_b)
      return 100.0 if string_a == string_b
      score = 0
      total_weight = algorithms.values.inject { |sum, weight| sum + weight }
      algorithms.each do |algorithm, weight|
        next unless weight.positive?
        score+= string_a.send("#{algorithm}_similarity", string_b) * weight
      end
      score / total_weight
    end
612790
static function isExistString( $sStr, $bTrim = false )
	{
		$bRet	= false;

		if ( ! is_bool( $bTrim ) )
		{
			return false;
		}

		if ( is_string( $sStr ) || is_numeric( $sStr ) )
		{
			$sStr	= $bTrim ? strval( trim( $sStr ) ) : strval( $sStr );
			$bRet	= ( strlen( $bTrim ? trim( $sStr ) : $sStr ) > 0 );
		}

		return $bRet;
	}
1176034
function _substr($string, $start, $length = null) {
        static $mbstring;

        // Cache expensive function_exists() calls.
        if (!isset($mbstring)) {
            $mbstring = function_exists('mb_substr');
        }

        if (is_null($length)) {
            $length = $this->_strlen($string);
        }
        if ($mbstring) {
            $ret = @mb_substr($string, $start, $length, $this->_charset);
            if (!empty($ret)) {
                return $ret;
            }
        }
        return substr($string, $start, $length);
    }
34424
public static boolean compareNullableString(@Nullable String string, SoyValue other) {
    // This is a parallel version of SharedRuntime.compareString except it can handle a null LHS.

    // This follows similarly to the Javascript specification, to ensure similar operation
    // over Javascript and Java: http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3
    if (other instanceof StringData || other instanceof SanitizedContent) {
      return Objects.equals(string, other.toString());
    }
    if (other instanceof NumberData) {
      if (string == null) {
        return false;
      }

      try {
        // Parse the string as a number.
        return Double.parseDouble(string) == other.numberValue();
      } catch (NumberFormatException nfe) {
        // Didn't parse as a number.
        return false;
      }
    }
    return false;
  }
1061454
public static function compare($str1, $str2, $ignoreCase = true)
    {
        if ($ignoreCase) {
            if (strcasecmp($str1, $str2) == 0) {
                return true;
            }
        } else {
            if (strcmp($str1, $str2) == 0) {
                return true;
            }
        }

        return false;
    }
387382
public function isEqual(array $instance_array)
    {
        $this->rewind();
        while (!empty($this->current())) {
            if ($this->hasEqualArrays($this->current()->toArray(), $instance_array)) {
                return true;
            }
            $this->next();
        }
        $this->rewind();
        return false;
    }
1024240
public boolean areEquals(String str1, String str2) {

		return (str1 == null && str2 == null || str1 != null && str2 != null && str1.equals(str2));
	}
693134
protected function tokensEqual($a, $b)
    {
        if (is_string($a) && is_string($b)) {
            return $a === $b;
        } elseif (isset($a[0], $a[1], $b[0], $b[1])) {
            return $a[0] === $b[0] && $a[1] == $b[1];
        }

        return false;
    }
759388
def compare(crypted_string, plain_string)
      BCrypt::Password.new(crypted_string).is_password?(plain_string)
    rescue BCrypt::Errors::InvalidHash
      false
    end
955410
public static function compare($known, $user)
    {
        if (strlen($known) !== strlen($user)) {
            return false;
        }
        $result      = 0;
        $knownLength = strlen($known);
        for ($i = 0; $i < $knownLength; $i++) {
            $result |= ord($known[$i]) ^ ord($user[$i]);
        }

        return $result == 0;
    }
1345214
private function hash_equals($knownString, $userString)
    {
        if (function_exists('\hash_equals')) {
            return \hash_equals($knownString, $userString);
        }
        if (strlen($knownString) !== strlen($userString)) {
            return false;
        }
        $len = strlen($knownString);
        $result = 0;
        for ($i = 0; $i < $len; $i++) {
            $result |= (ord($knownString[$i]) ^ ord($userString[$i]));
        }
        // They are only identical strings if $result is exactly 0...
        return 0 === $result;
    }
627072
public static function ensure_start_with($string, $leading_string)
    {
        if (StringTool :: start_with($string, $leading_string))
        {
            return $string;
        }
        else
        {
            return $leading_string . $string;
        }
    }