jbe@10: # Copyright (c) 2009 Public Software Group e. V., Berlin, Germany jbe@7: # jbe@7: # Permission is hereby granted, free of charge, to any person obtaining a jbe@7: # copy of this software and associated documentation files (the "Software"), jbe@7: # to deal in the Software without restriction, including without limitation jbe@7: # the rights to use, copy, modify, merge, publish, distribute, sublicense, jbe@7: # and/or sell copies of the Software, and to permit persons to whom the jbe@7: # Software is furnished to do so, subject to the following conditions: jbe@7: # jbe@7: # The above copyright notice and this permission notice shall be included in jbe@7: # all copies or substantial portions of the Software. jbe@7: # jbe@7: # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR jbe@7: # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, jbe@7: # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE jbe@7: # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER jbe@7: # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING jbe@7: # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER jbe@7: # DEALINGS IN THE SOFTWARE. jbe@0: jbe@7: jbe@7: # jbe@7: # File name: ruby/utf8proc.rb jbe@7: # jbe@7: # Description: jbe@7: # Part of the ruby wrapper for libutf8proc, which is written in ruby. jbe@7: # jbe@0: jbe@0: jbe@0: require 'utf8proc_native' jbe@0: jbe@2: jbe@0: module Utf8Proc jbe@2: jbe@0: SpecialChars = { jbe@0: :HT => "\x09", jbe@0: :LF => "\x0A", jbe@0: :VT => "\x0B", jbe@0: :FF => "\x0C", jbe@0: :CR => "\x0D", jbe@0: :FS => "\x1C", jbe@0: :GS => "\x1D", jbe@0: :RS => "\x1E", jbe@0: :US => "\x1F", jbe@0: :LS => "\xE2\x80\xA8", jbe@0: :PS => "\xE2\x80\xA9", jbe@0: } jbe@2: jbe@2: module StringExtensions jbe@2: def utf8map(*option_array) jbe@2: options = 0 jbe@2: option_array.each do |option| jbe@2: flag = Utf8Proc::Options[option] jbe@2: raise ArgumentError, "Unknown argument given to String#utf8map." unless jbe@2: flag jbe@2: options |= flag jbe@2: end jbe@2: return Utf8Proc::utf8map(self, options) jbe@2: end jbe@2: def utf8map!(*option_array) jbe@2: self.replace(self.utf8map(*option_array)) jbe@2: end jbe@2: def utf8nfd; utf8map( :stable, :decompose); end jbe@2: def utf8nfd!; utf8map!(:stable, :decompose); end jbe@2: def utf8nfc; utf8map( :stable, :compose); end jbe@2: def utf8nfc!; utf8map!(:stable, :compose); end jbe@2: def utf8nfkd; utf8map( :stable, :decompose, :compat); end jbe@2: def utf8nfkd!; utf8map!(:stable, :decompose, :compat); end jbe@2: def utf8nfkc; utf8map( :stable, :compose, :compat); end jbe@2: def utf8nfkc!; utf8map!(:stable, :compose, :compat); end jbe@3: def utf8chars jbe@3: result = self.utf8map(:charbound).split("\377") jbe@6: result.shift if result.first == "" jbe@3: result jbe@3: end jbe@2: def char_ary jbe@3: # depecated, use String#utf8chars instead jbe@3: utf8chars jbe@2: end jbe@2: end jbe@2: jbe@2: module IntegerExtensions jbe@2: def utf8 jbe@2: return Utf8Proc::utf8char(self) jbe@2: end jbe@2: end jbe@2: jbe@0: end jbe@0: jbe@2: jbe@0: class String jbe@2: include(Utf8Proc::StringExtensions) jbe@0: end jbe@0: jbe@0: class Integer jbe@2: include(Utf8Proc::IntegerExtensions) jbe@0: end jbe@0: