Skip to content

Commit

Permalink
[+]: add "has_whitespace"
Browse files Browse the repository at this point in the history
  • Loading branch information
voku committed Dec 2, 2019
1 parent c374fdf commit c5aae76
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/voku/helper/UTF8.php
Expand Up @@ -2333,6 +2333,24 @@ public static function has_lowercase(string $str): bool
return self::str_matches_pattern($str, '.*[[:lower:]]');
}

/**
* Returns true if the string contains whitespace, false otherwise.
*
* @param string $str <p>The input string.</p>
*
* @return bool
* <p>Whether or not the string contains whitespace.</p>
*/
public static function has_whitespace(string $str): bool
{
if (self::$SUPPORT['mbstring'] === true) {
/** @noinspection PhpComposerExtensionStubsInspection */
return \mb_ereg_match('.*[[:space:]]', $str);
}

return self::str_matches_pattern($str, '.*[[:space:]]');
}

/**
* Returns true if the string contains an upper case char, false otherwise.
*
Expand Down
55 changes: 55 additions & 0 deletions tests/Utf8TestsFromStringyTest.php
Expand Up @@ -138,6 +138,26 @@ public function charsProvider(): \Iterator
yield [['F', 'ò', 'ô', ' ', 'B', 'à', 'ř'], 'Fòô Bàř'];
}

public function hasWhitespaceProvider(): \Iterator
{
yield ['foo bar', ' foo bar '];
yield ['test string', 'test string'];
yield ['Ο συγγραφέας', ' Ο συγγραφέας '];
yield ['123' . "\n", ' 123 '];
yield [' ', ' ', 'UTF-8'];
// no-break space (U+00A0)
yield [' ', '           '];
// spaces U+2000 to U+200A
yield [' ', ' ', 'UTF-8'];
// narrow no-break space (U+202F)
yield [' ', ' ', 'UTF-8'];
// medium mathematical space (U+205F)
yield [' ', ' ', 'UTF-8'];
// ideographic space (U+3000)
yield ['1 2 3', ' 1 2  3  '];
yield [' ', ' '];
}

public function collapseWhitespaceProvider(): \Iterator
{
yield ['foo bar', ' foo bar '];
Expand Down Expand Up @@ -1968,6 +1988,41 @@ public function testHasLowerCase($expected, $str)
static::assertSame($expected, $result);
}

/**
* @dataProvider hasWhitespaceProvider()
*
* @param $str1
* @param $str2
*/
public function testHasWhitespace($str1, $str2)
{
$result = UTF8::has_whitespace($str1);

static::assertInternalType('boolean', $result);
static::assertSame(true, $result, 'tested: ' . $str1);

// ---

$result = UTF8::has_whitespace($str2);

static::assertInternalType('boolean', $result);
static::assertSame(true, $result, 'tested: ' . $str2);

// ---

$result = UTF8::has_whitespace('');

static::assertInternalType('boolean', $result);
static::assertSame(false, $result);

// ---

$result = UTF8::has_whitespace('abc-öäü');

static::assertInternalType('boolean', $result);
static::assertSame(false, $result);
}

/**
* @dataProvider hasUpperCaseProvider()
*
Expand Down

0 comments on commit c5aae76

Please sign in to comment.