Class: Ferret::Utils::MultiMapper

Summary

A MultiMapper performs a list of mappings from one string to another. You could of course just use gsub to do this but when you are just mapping strings, this is much faster.

Note that MultiMapper is immutable.

Example

   mapping = {
     ['à','á','â','ã','ä','å','ā','ă']         => 'a',
     'æ'                                       => 'ae',
     ['ď','đ']                                 => 'd',
     ['ç','ć','č','ĉ','ċ']                     => 'c',
     ['è','é','ê','ë','ē','ę','ě','ĕ','ė',]    => 'e',
     ['ƒ']                                     => 'f',
     ['ĝ','ğ','ġ','ģ']                         => 'g',
     ['ĥ','ħ']                                 => 'h',
     ['ì','ì','í','î','ï','ī','ĩ','ĭ']         => 'i',
     ['į','ı','ij','ĵ']                         => 'j',
     ['ķ','ĸ']                                 => 'k',
     ['ł','ľ','ĺ','ļ','ŀ']                     => 'l',
     ['ñ','ń','ň','ņ','ʼn','ŋ']                 => 'n',
     ['ò','ó','ô','õ','ö','ø','ō','ő','ŏ','ŏ'] => 'o',
     ['œ']                                     => 'oek',
     ['ą']                                     => 'q',
     ['ŕ','ř','ŗ']                             => 'r',
     ['ś','š','ş','ŝ','ș']                     => 's',
     ['ť','ţ','ŧ','ț']                         => 't',
     ['ù','ú','û','ü','ū','ů','ű','ŭ','ũ','ų'] => 'u',
     ['ŵ']                                     => 'w',
     ['ý','ÿ','ŷ']                             => 'y',
     ['ž','ż','ź']                             => 'z'
   mapper = MultiMapper.new(mapping)
   mapped_string = mapper.map(string)

Public Class Methods


MultiMapper.new() → new_multi_mapper

Returns a new multi-mapper object and compiles it for optimization.

Note that MultiMapper is immutable.

/*
 *  call-seq:
 *     MultiMapper.new() -> new_multi_mapper
 *  
 *  Returns a new multi-mapper object and compiles it for optimization.
 *
 *  Note that MultiMapper is immutable.
 */
static VALUE 
frb_mulmap_init(VALUE self, VALUE rmappings)
{
    MultiMapper *mulmap = DATA_PTR(self);
    rb_hash_foreach(rmappings, frb_mulmap_add_mappings_i, (VALUE)mulmap);
    mulmap_compile(mulmap);

    return self;
}

Public Instance Methods


multi_mapper.map(string) → mapped_string

Performs all the mappings on the string.

/*
 *  call-seq:
 *     multi_mapper.map(string) -> mapped_string
 *  
 *  Performs all the mappings on the string.
 */
VALUE
frb_mulmap_map(VALUE self, VALUE rstring)
{
    MultiMapper *mulmap = DATA_PTR(self);
    char *string = rs2s(rb_obj_as_string(rstring));
    char *mapped_string = mulmap_dynamic_map(mulmap, string);
    VALUE rmapped_string = rb_str_new2(mapped_string);
    free(mapped_string);
    return rmapped_string;
}