utf8proc
view ruby/utf8proc.rb @ 17:47b467f4c128
Contribution from libmojibake fork (missing file "normtest.c")
| author | Jiahao Chen, Steven G. Johnson, Anthony David Kelman | 
|---|---|
| date | Mon Dec 01 14:32:19 2014 -0500 (2014-12-01) | 
| parents | 00d2bcbdc945 | 
| children | 
 line source
     1 #  Copyright (c) 2009 Public Software Group e. V., Berlin, Germany
     2 #
     3 #  Permission is hereby granted, free of charge, to any person obtaining a
     4 #  copy of this software and associated documentation files (the "Software"),
     5 #  to deal in the Software without restriction, including without limitation
     6 #  the rights to use, copy, modify, merge, publish, distribute, sublicense,
     7 #  and/or sell copies of the Software, and to permit persons to whom the
     8 #  Software is furnished to do so, subject to the following conditions:
     9 #
    10 #  The above copyright notice and this permission notice shall be included in
    11 #  all copies or substantial portions of the Software.
    12 #
    13 #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14 #  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15 #  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16 #  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17 #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    18 #  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    19 #  DEALINGS IN THE SOFTWARE.
    22 #
    23 #  File name:    ruby/utf8proc.rb
    24 #
    25 #  Description:
    26 #  Part of the ruby wrapper for libutf8proc, which is written in ruby.
    27 #
    30 require 'utf8proc_native'
    33 module Utf8Proc
    35   SpecialChars = {
    36     :HT => "\x09",
    37     :LF => "\x0A",
    38     :VT => "\x0B",
    39     :FF => "\x0C",
    40     :CR => "\x0D",
    41     :FS => "\x1C",
    42     :GS => "\x1D",
    43     :RS => "\x1E",
    44     :US => "\x1F",
    45     :LS => "\xE2\x80\xA8",
    46     :PS => "\xE2\x80\xA9",
    47   }
    49   module StringExtensions
    50     def utf8map(*option_array)
    51       options = 0
    52       option_array.each do |option|
    53         flag = Utf8Proc::Options[option]
    54         raise ArgumentError, "Unknown argument given to String#utf8map." unless
    55           flag
    56         options |= flag
    57       end
    58       return Utf8Proc::utf8map(self, options)
    59     end
    60     def utf8map!(*option_array)
    61       self.replace(self.utf8map(*option_array))
    62     end
    63     def utf8nfd;   utf8map( :stable, :decompose); end
    64     def utf8nfd!;  utf8map!(:stable, :decompose); end
    65     def utf8nfc;   utf8map( :stable, :compose); end
    66     def utf8nfc!;  utf8map!(:stable, :compose); end
    67     def utf8nfkd;  utf8map( :stable, :decompose, :compat); end
    68     def utf8nfkd!; utf8map!(:stable, :decompose, :compat); end
    69     def utf8nfkc;  utf8map( :stable, :compose, :compat); end
    70     def utf8nfkc!; utf8map!(:stable, :compose, :compat); end
    71     def utf8chars
    72       result = self.utf8map(:charbound).split("\377")
    73       result.shift if result.first == ""
    74       result
    75     end
    76     def char_ary
    77       # depecated, use String#utf8chars instead
    78       utf8chars
    79     end
    80   end
    82   module IntegerExtensions
    83     def utf8
    84       return Utf8Proc::utf8char(self)
    85     end
    86   end
    88 end
    91 class String
    92   include(Utf8Proc::StringExtensions)
    93 end
    95 class Integer
    96   include(Utf8Proc::IntegerExtensions)
    97 end
