Configure to encode/decode shift-jis

This commit is contained in:
Adam Millerchip 2022-01-01 19:30:51 +09:00
parent 5742b11d82
commit 3d12f83330
4 changed files with 40 additions and 31 deletions

View file

@ -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 `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):
by adding `shift_jis` to your list of dependencies in `mix.exs`:
```elixir ```elixir
def deps do config :codepagex, :encodings, [
[ # CP392 is SHIFT_JIS
{:shift_jis, "~> 0.1.0"} # https://en.wikipedia.org/wiki/Code_page_932_(Microsoft_Windows)
] # Make sure to `mix deps.compile codepagex --force` after changing this
end "VENDORS/MICSFT/WINDOWS/CP932"
]
``` ```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) ## Encode/Decode
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/shift_jis>.
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
```

View file

@ -3,5 +3,6 @@ import Config
config :codepagex, :encodings, [ config :codepagex, :encodings, [
# CP392 is SHIFT_JIS # CP392 is SHIFT_JIS
# https://en.wikipedia.org/wiki/Code_page_932_(Microsoft_Windows) # 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" "VENDORS/MICSFT/WINDOWS/CP932"
] ]

View file

@ -1,18 +1,14 @@
defmodule ShiftJis do defmodule ShiftJis do
@moduledoc """ # Check config/config.exs to see how to enable this
Documentation for `ShiftJis`. @shift_jis "VENDORS/MICSFT/WINDOWS/CP932"
@doc ~S"""
iex> test = ShiftJis.encode("テスト")
<<131, 101, 131, 88, 131, 103>>
iex> ShiftJis.decode(test)
"テスト"
""" """
@doc """ def encode(str), do: Codepagex.from_string!(str, @shift_jis)
Hello world. def decode(str), do: Codepagex.to_string!(str, @shift_jis)
## Examples
iex> ShiftJis.hello()
:world
"""
def hello do
:world
end
end end

View file

@ -1,8 +1,4 @@
defmodule ShiftJisTest do defmodule ShiftJisTest do
use ExUnit.Case use ExUnit.Case
doctest ShiftJis doctest ShiftJis
test "greets the world" do
assert ShiftJis.hello() == :world
end
end end