lib/standard/: Added codecs group to Standard
authorLucas Bajolet <r4pass@hotmail.com>
Wed, 5 Aug 2015 17:17:54 +0000 (13:17 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Fri, 14 Aug 2015 17:34:43 +0000 (13:34 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/standard/codecs/codec_base.nit [new file with mode: 0644]
lib/standard/codecs/codecs.nit [new file with mode: 0644]
lib/standard/codecs/utf8.nit [new file with mode: 0644]

diff --git a/lib/standard/codecs/codec_base.nit b/lib/standard/codecs/codec_base.nit
new file mode 100644 (file)
index 0000000..b4a9523
--- /dev/null
@@ -0,0 +1,51 @@
+# 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.
+
+# Base for codecs to use with streams
+#
+# A Codec (Coder/Decoder) is a tranformer from a byte-format to another
+#
+# As Nit Strings are UTF-8, a codec works as :
+# - Coder: From a UTF-8 string to a specified format (writing)
+# - Decoder: From a specified format to a UTF-8 string (reading)
+module codec_base
+
+import text
+import bytes
+
+# Codes UTF-8 entities to an external format
+abstract class Coder
+
+       # Transforms `c` to its representation in the format of `self`
+       fun code_char(c: Char): Bytes is abstract
+
+       # Adds a char `c` to bytes `s`
+       fun add_char_to(c: Char, s: Bytes) is abstract
+
+       # Transforms `s` to the format of `self`
+       fun code_string(s: Text): Bytes is abstract
+
+       # Adds a string `s` to bytes `b`
+       fun add_string_to(s: Text, b: Bytes) is abstract
+end
+
+# Decodes entities in an external format to UTF-8
+abstract class Decoder
+
+       # Decodes a char from `b` to a Unicode code-point
+       fun decode_char(b: Bytes): Char is abstract
+
+       # Decodes a string `b` to UTF-8
+       fun decode_string(b: Bytes): String is abstract
+end
diff --git a/lib/standard/codecs/codecs.nit b/lib/standard/codecs/codecs.nit
new file mode 100644 (file)
index 0000000..25e9931
--- /dev/null
@@ -0,0 +1,19 @@
+# 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.
+
+# Group module for all codec-related manipulations
+module codecs
+
+import codec_base
+import utf8
diff --git a/lib/standard/codecs/utf8.nit b/lib/standard/codecs/utf8.nit
new file mode 100644 (file)
index 0000000..65f2fc9
--- /dev/null
@@ -0,0 +1,50 @@
+# 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.
+
+# Codec for UTF-8 I/O
+module utf8
+
+import codec_base
+
+# Returns UTF-8 entities as-is
+private class UTF8Coder
+       super Coder
+
+       redef fun code_char(c) do return c.to_s.to_bytes
+
+       redef fun add_char_to(c, stream) do c.to_s.append_to_bytes(stream)
+
+       redef fun code_string(s) do return s.to_bytes
+
+       redef fun add_string_to(s, b) do s.append_to_bytes(b)
+end
+
+# Decodes entities in an external format to UTF-8
+private class UTF8Decoder
+       super Decoder
+
+       redef fun decode_char(b) do
+               var s = b.to_s
+               return s[0]
+       end
+
+       redef fun decode_string(b) do
+               return b.to_s
+       end
+end
+
+# Returns the instance of a UTF-8 Coder
+fun utf8_coder: Coder do return once new UTF8Coder
+# Returns the instance of a UTF-8 Decoder
+fun utf8_decoder: Decoder do return once new UTF8Decoder