return slice(from, length)
end
+ # Reverse the byte array in place
+ #
+ # var b = "abcd".to_bytes
+ # b.reverse
+ # assert b.to_s == "dcba"
+ fun reverse
+ do
+ var l = length
+ for i in [0..l/2[ do
+ var tmp = self[i]
+ self[i] = self[l-i-1]
+ self[l-i-1] = tmp
+ end
+ end
+
# Returns self as an hexadecimal digest.
#
# Also known as plain hexdump or postscript hexdump.
redef fun successor(i) is intern
redef fun predecessor(i) is intern
+ # The `i`-th char after self (in code point)
+ #
+ # ~~~
+ # assert 'A' + 5 == 'F'
+ # ~~~
+ #
+ # Alias of `successor`.
+ fun +(i: Int): Char do return successor(i)
+
+ # The `i`-th char before self (in code point)
+ #
+ # ~~~
+ # assert 'F' - 5 == 'A'
+ # ~~~
+ #
+ # Alias of `predecessor`.
+ fun -(i: Int): Char do return predecessor(i)
+
redef fun distance(c)
do
var d = self.code_point - c.code_point
end
return arr.to_s
end
+
+ # Vigenere encoder on ASCII letters.
+ #
+ # Only the letters in '[A-Za-z]' are encoded while keeping the case.
+ #
+ # assert "Hello, World!".vigenere("abc") == "Hfnlp, Yosnd!"
+ #
+ # REQUIRE `key` contains only lowercases '[a-z]'
+ fun vigenere(key: String): String
+ do
+ var res = new Buffer
+ res.enlarge(length)
+ var i = 0
+ for c in self do
+ var k = key[i%key.length]
+ assert k >= 'a' and k <= 'z'
+ if c.is_letter then
+ var d = k.code_point - 'a'.code_point
+ c = c.rot(d)
+ i += 1
+ end
+ res.add c
+ end
+ return res.to_s
+ end
+
+ # Vigenere decoder on ASCII letters.
+ #
+ # Only the letters in '[A-Za-z]' are decoded while keeping the case.
+ #
+ # assert "Hfnlp, Yosnd!".uvigenere("abc") == "Hello, World!"
+ #
+ # REQUIRE `key` contains only lowercases '[a-z]'
+ fun uvigenere(key: String): String
+ do
+ var res = new Buffer
+ res.enlarge(length)
+ var i = 0
+ for c in self do
+ var k = key[i%key.length]
+ assert k >= 'a' and k <= 'z'
+ if c.is_letter then
+ var d = k.code_point - 'a'.code_point
+ c = c.rot(-d)
+ i += 1
+ end
+ res.add c
+ end
+ return res.to_s
+ end
end
redef class Bytes
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Mix of utilities and services related to bytes
+module bytes
+
+redef class Bytes
+ # PKCS#7 padding.
+ #
+ # Extends `self` by appending the number of bytes of padding to the end of the block.
+ #
+ # assert "YELLOW SUBMARINE".to_bytes.pkcs7(20) == "YELLOW SUBMARINE\x04\x04\x04\x04".to_bytes
+ fun pkcs7(blocksize: Int): Bytes
+ do
+ var mod = length % blocksize
+ if mod == 0 then return self
+ var pad = blocksize - mod
+ var byte = pad.to_b
+ for i in [0..pad[ do add byte
+ return self
+ end
+end
import basic_ciphers
import xor_ciphers
+import bytes
../lib/core/kernel.nit:517,1--599,3: Error: `kernel$Float` does not specialize `module_0$Object`. Possible duplication of the root class `Object`?
../lib/core/kernel.nit:601,1--705,3: Error: `kernel$Byte` does not specialize `module_0$Object`. Possible duplication of the root class `Object`?
../lib/core/kernel.nit:707,1--885,3: Error: `kernel$Int` does not specialize `module_0$Object`. Possible duplication of the root class `Object`?
-../lib/core/kernel.nit:887,1--1056,3: Error: `kernel$Char` does not specialize `module_0$Object`. Possible duplication of the root class `Object`?
-../lib/core/kernel.nit:1058,1--1075,3: Error: `kernel$Pointer` does not specialize `module_0$Object`. Possible duplication of the root class `Object`?
-../lib/core/kernel.nit:1077,1--1086,3: Error: `kernel$Task` does not specialize `module_0$Object`. Possible duplication of the root class `Object`?
+../lib/core/kernel.nit:887,1--1074,3: Error: `kernel$Char` does not specialize `module_0$Object`. Possible duplication of the root class `Object`?
+../lib/core/kernel.nit:1076,1--1093,3: Error: `kernel$Pointer` does not specialize `module_0$Object`. Possible duplication of the root class `Object`?
+../lib/core/kernel.nit:1095,1--1104,3: Error: `kernel$Task` does not specialize `module_0$Object`. Possible duplication of the root class `Object`?
Numeric -> Int [dir=back arrowtail=open style=dashed];
Char [
- label = "{Char||+ to_i(): Int\l+ ascii(): Byte\l+ code_point(): Int\l+ is_ascii(): Bool\l+ to_lower(): Char\l+ to_upper(): Char\l+ is_digit(): Bool\l+ is_lower(): Bool\l+ is_upper(): Bool\l+ is_letter(): Bool\l+ is_whitespace(): Bool\l}"
+ label = "{Char||+ +(i: Int): Char\l+ -(i: Int): Char\l+ to_i(): Int\l+ ascii(): Byte\l+ code_point(): Int\l+ is_ascii(): Bool\l+ to_lower(): Char\l+ to_upper(): Char\l+ is_digit(): Bool\l+ is_lower(): Bool\l+ is_upper(): Bool\l+ is_letter(): Bool\l+ is_whitespace(): Bool\l}"
]
Discrete -> Char [dir=back arrowtail=open style=dashed];
Numeric -> Int [dir=back arrowtail=open style=dashed];
Char [
- label = "{Char||+ to_i(): Int\l+ ascii(): Byte\l+ code_point(): Int\l+ is_ascii(): Bool\l+ to_lower(): Char\l+ to_upper(): Char\l+ is_digit(): Bool\l+ is_lower(): Bool\l+ is_upper(): Bool\l+ is_letter(): Bool\l+ is_whitespace(): Bool\l}"
+ label = "{Char||+ +(i: Int): Char\l+ -(i: Int): Char\l+ to_i(): Int\l+ ascii(): Byte\l+ code_point(): Int\l+ is_ascii(): Bool\l+ to_lower(): Char\l+ to_upper(): Char\l+ is_digit(): Bool\l+ is_lower(): Bool\l+ is_upper(): Bool\l+ is_letter(): Bool\l+ is_whitespace(): Bool\l}"
]
Discrete -> Char [dir=back arrowtail=open style=dashed];