Project

charshift

0.0
No commit activity in last 3 years
No release in over 3 years
Charshift is a simple gem which adds functionality to the String class. It's primary function is to act on a given string, taking a fixnum parameter, then shifting each character in that string to a higher or lower ordinal position in that strings encoding. Charshift works with all of Ruby's included encodings and also works with devloper supplied 'custom encodings.' Simply provide an ordered set of characters as an optional parameter and charshift will work on the string using that set instead of the strings native encoding. Charshift also includes a '.get_encoding_length' method which returns the number of of characters which a given strings encoding contains. Finally, strings can be shifted in place using the '.charshift!' method.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

Charshift

What does this do?

Charshift, as the name suggests, shifts characters. More specifically each character has an ordinal indicator (raw number) which denotes its position for the strings character encoding. For example, in Rubys default UTF-8 character encoding, the character "S" has an ordinal value of 83. This encoding contains thousands of characters, what charshift does is takes an input string and shifts each character in the string to a higher or lower position in the encoding.

Installation

Charshift is available as a gem, to install it run:

gem install charshift

Or, if you're using Bundler, add it to your Gemfile:

gem 'charshift'

And run:

$ bundle install

Usage

Charshift adds several methods to the String class.

To shift characters left or right simply call .charshift on any string object and pass it a positive or negative fixnum value. For example:

 > "Hello, World!".charshift(10)
=> "Rovvy6*ay|vn+" 

This shifts all values ten places to the 'right,' that is, each character output is it's original ordinal potion plus ten.

If the value passed is larger than the character set for the strings encoding the method will loop over the set and continue from the other side. Charshift works will all of Rubys included encodings so if you're using something like Big5-HKSCS it will work as well as it does with the default UTF-8 character set.

Charshift also supports "custom encodings." If you wish to use your own list of characters simply pass that list in the form an array as an optional second parameter:

 > list = ["a", "W", "g", "d", " ", "H", "l", "k", "e", "o", "t", "!", ",", "r", "}", "D", "Y"]
 > "Hello, World!".charshift(10, list)
=> "DWYYgH}!glYr "

The value returned does not modify the original string, if you want to modify the string in place simply call '.charshift!' on the target string.

Finally, if you just want to see the number of characters for a strings encoding, use the '.get_encoding_length' method.

 > "Hello, World!".get_encoding_length
=> 1114112
 > "Hello, World!".encode("ISO-2022-JP").get_encoding_length
=> 256