From 509cbc51eb3984b4a67076ee88df871ea157c46c Mon Sep 17 00:00:00 2001 From: chasonr Date: Sun, 23 Mar 2014 21:46:00 -0400 Subject: Implement \u notation for strings and regexes. This change adds the \u notation for double quoted strings and regular expressions. It does not implement the \u notation for character literals. Both the \uNNNN and \u{NNNN} notations are supported. \uNNNN is implemented by emitting equivalent UTF-8; that is, "\u4000" is equivalent to "\xE4\x80\x80". Unlike CRuby, the \u{NNNN} notation allows only one character per pair of braces; I see no way to lift this restriction without remodeling the parser. --- test/t/unicode.rb | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 test/t/unicode.rb (limited to 'test') diff --git a/test/t/unicode.rb b/test/t/unicode.rb new file mode 100644 index 000000000..8f94421e6 --- /dev/null +++ b/test/t/unicode.rb @@ -0,0 +1,60 @@ +# Test of the \u notation + +assert('bare \u notation test') do + # Mininum and maximum one byte characters + assert_equal("\u0000", "\x00") + assert_equal("\u007F", "\x7F") + + # Mininum and maximum two byte characters + assert_equal("\u0080", "\xC2\x80") + assert_equal("\u07FF", "\xDF\xBF") + + # Mininum and maximum three byte characters + assert_equal("\u0800", "\xE0\xA0\x80") + assert_equal("\uFFFF", "\xEF\xBF\xBF") + + # Four byte characters require the \U notation +end + +assert('braced \u notation test') do + # Mininum and maximum one byte characters + assert_equal("\u{0000}", "\x00") + assert_equal("\u{007F}", "\x7F") + + # Mininum and maximum two byte characters + assert_equal("\u{0080}", "\xC2\x80") + assert_equal("\u{07FF}", "\xDF\xBF") + + # Mininum and maximum three byte characters + assert_equal("\u{0800}", "\xE0\xA0\x80") + assert_equal("\u{FFFF}", "\xEF\xBF\xBF") + + # Mininum and maximum four byte characters + assert_equal("\u{10000}", "\xF0\x90\x80\x80") + assert_equal("\u{10FFFF}", "\xF4\x8F\xBF\xBF") +end + +# Test regular expressions only if implemented +begin + Regexp + have_regexp = true +rescue NameError + have_regexp = false +end +if have_regexp then + assert('Testing in regular expressions') do + # The regular expression uses the unbraced notation where the string uses + # the braced notation, and vice versa, so these tests will fail if the \u + # modification is not applied + + # Test of unbraced \u notation in a regular expression + assert_false(/\u0300/ =~ "\u{02FF}") + assert_true( /\u0300/ =~ "\u{0300}") + assert_false(/\u0300/ =~ "\u{0301}") + + # Test of braced \u notation in a regular expression + assert_false(/\u{0300}/ =~ "\u02FF") + assert_true( /\u{0300}/ =~ "\u0300") + assert_false(/\u{0300}/ =~ "\u0301") + end +end -- cgit v1.2.3 From 8162295ba25adfdf3135b61e89629c46fcc19472 Mon Sep 17 00:00:00 2001 From: chasonr Date: Sun, 23 Mar 2014 22:31:49 -0400 Subject: Small correction to the test identification. --- test/t/unicode.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/t/unicode.rb b/test/t/unicode.rb index 8f94421e6..a8e8c0e14 100644 --- a/test/t/unicode.rb +++ b/test/t/unicode.rb @@ -42,7 +42,7 @@ rescue NameError have_regexp = false end if have_regexp then - assert('Testing in regular expressions') do + assert('Testing \u in regular expressions') do # The regular expression uses the unbraced notation where the string uses # the braced notation, and vice versa, so these tests will fail if the \u # modification is not applied -- cgit v1.2.3