From c5aae7623a5723d7f19fff1c43e9c05ed162b1db Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Mon, 2 Dec 2019 01:59:01 +0100 Subject: [PATCH] [+]: add "has_whitespace" -> https://github.com/voku/Stringy/issues/23 --- src/voku/helper/UTF8.php | 18 ++++++++++ tests/Utf8TestsFromStringyTest.php | 55 ++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/src/voku/helper/UTF8.php b/src/voku/helper/UTF8.php index f6985bd6..a405a35d 100644 --- a/src/voku/helper/UTF8.php +++ b/src/voku/helper/UTF8.php @@ -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

The input string.

+ * + * @return bool + *

Whether or not the string contains whitespace.

+ */ + 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. * diff --git a/tests/Utf8TestsFromStringyTest.php b/tests/Utf8TestsFromStringyTest.php index 47fc4eb1..fa8393d1 100644 --- a/tests/Utf8TestsFromStringyTest.php +++ b/tests/Utf8TestsFromStringyTest.php @@ -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 ']; @@ -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() *