diff options
-rw-r--r-- | src/Util.php | 11 | ||||
-rw-r--r-- | test/UtilTest.php | 27 |
2 files changed, 38 insertions, 0 deletions
diff --git a/src/Util.php b/src/Util.php index 0f1499a..47a26a4 100644 --- a/src/Util.php +++ b/src/Util.php @@ -24,6 +24,17 @@ function throw_if_null($arg) { if ($arg === null) throw new Exception('argument is null'); } +function has_rfc822_specials($str) { + // check for RFC822 specials (see section 3.3) + return strpbrk($str, '()<>@,;:\".[]') !== false; +} + +function rfc822_quote($str) { + // turn \ and " into quoted-pairs, then quote the whole string + return '"' . str_replace(array("\\", "\"",), + array("\\\\", "\\\""), $str) . '"'; +} + function is_ascii($str) { return !preg_match('/[^[:ascii:]]/', $str); } diff --git a/test/UtilTest.php b/test/UtilTest.php index 7135b9f..1cf8eac 100644 --- a/test/UtilTest.php +++ b/test/UtilTest.php @@ -5,6 +5,33 @@ require_once('PHPUnit/Framework.php'); require_once(__DIR__ . '/../src/Util.php'); class UtilTest extends PHPUnit_Framework_TestCase { + + public function has_rfc822_specials_provider() { + return array( + array('Joel Uckelman', false), + array('L.Tankersley', true), + array('()<>@,;:\".[]', true) + ); + } + + /** @dataProvider has_rfc822_specials_provider */ + public function test_has_rfc822_specials($string, $expected) { + $this->assertEquals($expected, has_rfc_822_specials($string)); + } + + public function rfc822_quote_provider() { + return array( + array('Joel Uckelman', 'Joel Uckelman'), + array('L.Tankersley', '"L.Tankersley"'), + array('"\foo\"', '"\"\\foo\\\""') + ); + } + + /** @dataProvider rfc822_quote_provider */ + public function test_rfc822_quote($string, $expected) { + $this->assertEquals($expected, rfc_822_quote($string)); + } + public function is_ascii_provider() { return array( array('', true), |