squarecoder 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE.txt +1 -1
- data/README.rdoc +2 -2
- data/lib/squarecoder.rb +2 -2
- data/lib/squarecoder/core_ext/string.rb +12 -12
- data/lib/squarecoder/errors.rb +1 -1
- data/lib/squarecoder/transcoder.rb +11 -13
- metadata +3 -3
data/LICENSE.txt
CHANGED
data/README.rdoc
CHANGED
@@ -5,7 +5,7 @@ Squarecoder encapsulates a Squarecoder::Transcoder singleton that encodes and de
|
|
5
5
|
== Installation
|
6
6
|
|
7
7
|
$ gem install squarecoder
|
8
|
-
|
8
|
+
|
9
9
|
Or
|
10
10
|
|
11
11
|
# in Gemfile
|
@@ -22,5 +22,5 @@ Or
|
|
22
22
|
|
23
23
|
== Copyright
|
24
24
|
|
25
|
-
Copyright (c) 2011 Aaron Breckenridge. See LICENSE.txt for
|
25
|
+
Copyright (c) 2011-2013 Aaron Breckenridge. See LICENSE.txt for
|
26
26
|
further details.
|
data/lib/squarecoder.rb
CHANGED
@@ -2,8 +2,8 @@ require 'rubygems'
|
|
2
2
|
require 'singleton'
|
3
3
|
|
4
4
|
module Squarecoder
|
5
|
-
|
6
|
-
|
5
|
+
# Maximum length of an input string
|
6
|
+
MAX_LEN = 81
|
7
7
|
require 'squarecoder/core_ext/string'
|
8
8
|
require 'squarecoder/errors'
|
9
9
|
require 'squarecoder/transcoder'
|
@@ -1,16 +1,16 @@
|
|
1
1
|
class String
|
2
|
-
|
2
|
+
|
3
3
|
# Convert a String to a Grouped array of Characters of a specific length
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# Example:
|
6
|
-
#
|
6
|
+
#
|
7
7
|
# "abcdefgh".in_character_arrays_of_length(3) # => [["a","b","c"],["d","e","f"],["g","h"]]
|
8
|
-
#
|
8
|
+
#
|
9
9
|
# Returns an Array.
|
10
|
-
def in_character_arrays_of_length(
|
11
|
-
scan(
|
10
|
+
def in_character_arrays_of_length(length)
|
11
|
+
scan(/./).each_slice(length).to_a
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
# Encode a String
|
15
15
|
def square_encode
|
16
16
|
dup.square_encode!
|
@@ -18,17 +18,17 @@ class String
|
|
18
18
|
|
19
19
|
# Encode a String (in-place)
|
20
20
|
def square_encode!
|
21
|
-
replace(
|
21
|
+
replace(Squarecoder::Transcoder.encode(self))
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
# Decode an encoded String
|
25
25
|
def square_decode
|
26
26
|
dup.square_decode!
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
# Decode a string (in-place)
|
30
30
|
def square_decode!
|
31
|
-
replace(
|
31
|
+
replace(Squarecoder::Transcoder.decode(self))
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
end
|
data/lib/squarecoder/errors.rb
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
module Squarecoder
|
2
|
-
|
2
|
+
|
3
3
|
# A Transcoder is a Singleton used to encode and decode strings.
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# Examples
|
6
|
-
#
|
6
|
+
#
|
7
7
|
# Squarecoder::Transcoder.encode("haveaniceday") # => "hae and via ecy"
|
8
8
|
# Squarecoder::Transcoder.decode("hae and via ecy") # => "haveaniceday"
|
9
9
|
# "haveaniceday".square_encode # => "hae and via ecy"
|
10
10
|
# "hae and via ecy".square_decode # => "haveaniceday"
|
11
11
|
class Transcoder
|
12
|
-
|
13
|
-
include Singleton
|
14
|
-
|
12
|
+
|
15
13
|
# Encode a String in square code
|
16
14
|
def self.encode a_string, ignore_whitespace = false, ignore_max_length = false
|
17
15
|
if !ignore_max_length && a_string.length > MAX_LEN
|
@@ -20,19 +18,19 @@ module Squarecoder
|
|
20
18
|
if !ignore_whitespace && a_string.include?(' ')
|
21
19
|
raise Squarecoder::NoWhitespace
|
22
20
|
end
|
23
|
-
len = (
|
24
|
-
arry = a_string.in_character_arrays_of_length(
|
21
|
+
len = (a_string.length ** (0.5)).ceil
|
22
|
+
arry = a_string.in_character_arrays_of_length(len)
|
25
23
|
if arry.last.length != len
|
26
|
-
|
24
|
+
(len - arry.last.length).times { arry.last << ' ' } # a tiny bit faster than arry.fill
|
27
25
|
end
|
28
26
|
arry.transpose.collect { |line| line.join }.join(' ').rstrip
|
29
27
|
end
|
30
|
-
|
28
|
+
|
31
29
|
# Decode a String from square code
|
32
30
|
def self.decode a_string
|
33
|
-
encode(
|
31
|
+
encode(a_string, true, true).gsub(/ +/, '')
|
34
32
|
end
|
35
|
-
|
33
|
+
|
36
34
|
end
|
37
|
-
|
35
|
+
|
38
36
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: squarecoder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-02-07 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A library to encode and decode strings using the square code encryption
|
15
15
|
method.
|
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
47
|
version: 1.3.6
|
48
48
|
requirements: []
|
49
49
|
rubyforge_project:
|
50
|
-
rubygems_version: 1.8.
|
50
|
+
rubygems_version: 1.8.24
|
51
51
|
signing_key:
|
52
52
|
specification_version: 3
|
53
53
|
summary: encode and decode strings using the square code encryption method
|