utf8proc
view ruby/utf8proc.rb @ 0:a0368662434c
Version 0.1
| author | jbe | 
|---|---|
| date | Fri Jun 02 12:00:00 2006 +0200 (2006-06-02) | 
| parents | |
| children | 61a89ecc2fb9 | 
 line source
     1 ##
     2  #  Copyright (c) 2006, FlexiGuided GmbH, Berlin, Germany
     3  #  Author: Jan Behrens <jan.behrens@flexiguided.de>
     4  #  All rights reserved.
     5  #
     6  #  Redistribution and use in source and binary forms, with or without
     7  #  modification, are permitted provided that the following conditions are
     8  #  met:
     9  #
    10  #  1. Redistributions of source code must retain the above copyright
    11  #     notice, this list of conditions and the following disclaimer.
    12  #  2. Redistributions in binary form must reproduce the above copyright
    13  #     notice, this list of conditions and the following disclaimer in the
    14  #     documentation and/or other materials provided with the distribution.
    15  #  3. Neither the name of the FlexiGuided GmbH nor the names of its
    16  #     contributors may be used to endorse or promote products derived from
    17  #     this software without specific prior written permission.
    18  #
    19  #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    20  #  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    21  #  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    22  #  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    23  #  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    24  #  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    25  #  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    26  #  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    27  #  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    28  #  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    29  #  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    30  #
    31  ##
    34 ##
    35  #  File name:    ruby/utf8proc.rb
    36  #  Version:      0.1
    37  #  Last changed: 2006-05-31
    38  #
    39  #  Description:
    40  #  Part of the ruby wrapper for libutf8proc, which is written in ruby.
    41  ##
    44 require 'utf8proc_native'
    46 module Utf8Proc
    47   SpecialChars = {
    48     :HT => "\x09",
    49     :LF => "\x0A",
    50     :VT => "\x0B",
    51     :FF => "\x0C",
    52     :CR => "\x0D",
    53     :FS => "\x1C",
    54     :GS => "\x1D",
    55     :RS => "\x1E",
    56     :US => "\x1F",
    57     :LS => "\xE2\x80\xA8",
    58     :PS => "\xE2\x80\xA9",
    59   }
    60 end
    62 class String
    63   def utf8map(*option_array)
    64     options = 0
    65     option_array.each do |option|
    66       flag = Utf8Proc::Options[option]
    67       raise ArgumentError, "Unknown argument given to String#utf8map." unless
    68         flag
    69       options |= flag
    70     end
    71     return Utf8Proc::utf8map(self, options)
    72   end
    73   def utf8map!(*option_array)
    74     self.replace(self.utf8map(*option_array))
    75   end
    76   def utf8nfd;   utf8map( :stable); end
    77   def utf8nfd!;  utf8map!(:stable); end
    78   def utf8nfc;   utf8map( :stable, :compose); end
    79   def utf8nfc!;  utf8map!(:stable, :compose); end
    80   def utf8nfkd;  utf8map( :stable, :compat); end
    81   def utf8nfkd!; utf8map!(:stable, :compat); end
    82   def utf8nfkc;  utf8map( :stable, :compose, :compat); end
    83   def utf8nfkc!; utf8map!(:stable, :compose, :compat); end
    84 end
    86 class Integer
    87   def utf8
    88     return Utf8Proc::utf8char(self)
    89   end
    90 end
