From 3d12f8333096f7c23ba2925bebc7665977be8a2d Mon Sep 17 00:00:00 2001 From: Adam Millerchip Date: Sat, 1 Jan 2022 19:30:51 +0900 Subject: [PATCH] Configure to encode/decode shift-jis --- README.md | 42 ++++++++++++++++++++++++++++------------- config/config.exs | 1 + lib/shift_jis.ex | 24 ++++++++++------------- test/shift_jis_test.exs | 4 ---- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 054cde3..65a778f 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,37 @@ -# ShiftJis +# SHIFT_JIS in Elixir with Codepagex -**TODO: Add description** +A demonstration of how to encode/decode SHIFT_JIS in Elixir with the [Codepagex](https://hex.pm/packages/codepagex) library -## Installation +## Config -If [available in Hex](https://hex.pm/docs/publish), the package can be installed -by adding `shift_jis` to your list of dependencies in `mix.exs`: +`SHIFT_JIS` is called `VENDORS/MICSFT/WINDOWS/CP932` in Codepagex. Enable it in [config](https://github.com/adamu/shift-jis-elixir/blob/main/config/config.exs): ```elixir -def deps do - [ - {:shift_jis, "~> 0.1.0"} - ] -end +config :codepagex, :encodings, [ + # CP392 is SHIFT_JIS + # https://en.wikipedia.org/wiki/Code_page_932_(Microsoft_Windows) + # Make sure to `mix deps.compile codepagex --force` after changing this + "VENDORS/MICSFT/WINDOWS/CP932" +] ``` -Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) -and published on [HexDocs](https://hexdocs.pm). Once published, the docs can -be found at . +## Encode/Decode +After that we can [encode/decode](https://github.com/adamu/shift-jis-elixir/blob/main/lib/shift_jis.ex): + +```elixir +defmodule ShiftJis do + # Check config/config.exs to see how to enable this + @shift_jis "VENDORS/MICSFT/WINDOWS/CP932" + + @doc ~S""" + iex> test = ShiftJis.encode("テスト") + <<131, 101, 131, 88, 131, 103>> + iex> ShiftJis.decode(test) + "テスト" + """ + + def encode(str), do: Codepagex.from_string!(str, @shift_jis) + def decode(str), do: Codepagex.to_string!(str, @shift_jis) +end +``` diff --git a/config/config.exs b/config/config.exs index a5a6833..5de6190 100644 --- a/config/config.exs +++ b/config/config.exs @@ -3,5 +3,6 @@ import Config config :codepagex, :encodings, [ # CP392 is SHIFT_JIS # https://en.wikipedia.org/wiki/Code_page_932_(Microsoft_Windows) + # Make sure to `mix deps.compile codepagex --force` after changing this "VENDORS/MICSFT/WINDOWS/CP932" ] diff --git a/lib/shift_jis.ex b/lib/shift_jis.ex index aa6c496..4e556d4 100644 --- a/lib/shift_jis.ex +++ b/lib/shift_jis.ex @@ -1,18 +1,14 @@ defmodule ShiftJis do - @moduledoc """ - Documentation for `ShiftJis`. + # Check config/config.exs to see how to enable this + @shift_jis "VENDORS/MICSFT/WINDOWS/CP932" + + @doc ~S""" + iex> test = ShiftJis.encode("テスト") + <<131, 101, 131, 88, 131, 103>> + iex> ShiftJis.decode(test) + "テスト" """ - @doc """ - Hello world. - - ## Examples - - iex> ShiftJis.hello() - :world - - """ - def hello do - :world - end + def encode(str), do: Codepagex.from_string!(str, @shift_jis) + def decode(str), do: Codepagex.to_string!(str, @shift_jis) end diff --git a/test/shift_jis_test.exs b/test/shift_jis_test.exs index 40dd253..744a669 100644 --- a/test/shift_jis_test.exs +++ b/test/shift_jis_test.exs @@ -1,8 +1,4 @@ defmodule ShiftJisTest do use ExUnit.Case doctest ShiftJis - - test "greets the world" do - assert ShiftJis.hello() == :world - end end