├── .github └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE-MIT ├── README.md ├── lib └── mbcs.ex ├── mix.exs └── test ├── mbcs_test.exs └── test_helper.exs /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | name: Build and test 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - uses: actions/checkout@v2 17 | - name: Set up Elixir 18 | uses: actions/setup-elixir@v1 19 | with: 20 | elixir-version: '1.10.3' 21 | otp-version: '22.3' 22 | - name: Restore dependencies cache 23 | uses: actions/cache@v2 24 | with: 25 | path: deps 26 | key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }} 27 | restore-keys: ${{ runner.os }}-mix- 28 | - name: Install dependencies 29 | run: mix deps.get 30 | - name: Run tests 31 | run: mix test 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io 2 | 3 | ### OSX ### 4 | .DS_Store 5 | .AppleDouble 6 | .LSOverride 7 | 8 | # Icon must end with two \r 9 | Icon 10 | 11 | # Thumbnails 12 | ._* 13 | 14 | # Files that might appear on external disk 15 | .Spotlight-V100 16 | .Trashes 17 | 18 | # Directories potentially created on remote AFP share 19 | .AppleDB 20 | .AppleDesktop 21 | Network Trash Folder 22 | Temporary Items 23 | .apdisk 24 | 25 | 26 | ### vim ### 27 | [._]*.s[a-w][a-z] 28 | [._]s[a-w][a-z] 29 | *.un~ 30 | Session.vim 31 | .netrwhist 32 | *~ 33 | 34 | 35 | ### Elixir ### 36 | /_build 37 | /deps 38 | erl_crash.dump 39 | *.ez 40 | mix.lock 41 | 42 | 43 | ### Erlang ### 44 | .eunit 45 | deps 46 | *.o 47 | *.beam 48 | *.plt 49 | erl_crash.dump 50 | ebin 51 | rel/example_project 52 | .concrete/DEV_MODE 53 | .rebar 54 | 55 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 woxtu 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # elixir-mbcs 2 | 3 | Wrapper for [erlang-mbcs](https://code.google.com/p/erlang-mbcs/). 4 | This module provides functions for character encoding conversion. 5 | 6 | ## Install 7 | 8 | Adding the following to the mix.exs in your project: 9 | 10 | ```elixir 11 | defp deps do 12 | [ 13 | {:elixir_mbcs, github: "woxtu/elixir-mbcs", tag: "0.1.3"}, 14 | ] 15 | end 16 | ``` 17 | 18 | ## Usage 19 | 20 | ```elixir 21 | # Start mbcs server 22 | iex> Mbcs.start 23 | :ok 24 | 25 | # Convert UTF-8 to Shift_JIS 26 | iex> Mbcs.encode!("九条カレン", :cp932) 27 | <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>> 28 | 29 | # Convert Shift_JIS to UTF-8, and return as a list 30 | iex> Mbcs.decode!([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932, return: :list) 31 | [20061, 26465, 12459, 12524, 12531] 32 | ``` 33 | 34 | ## Support encodings 35 | 36 | * cp037 37 | * cp437 38 | * cp500 39 | * cp737, cp775 40 | * cp850, cp852, cp855, cp857, cp860, cp861, cp862, cp863, cp864, cp865, cp866, cp869, cp874, cp875 41 | * cp932, cp936, gbk, cp949, cp950, big5 42 | * cp1026, cp1250, cp1251, cp1252, cp1253, cp1254, cp1255, cp1256, cp1257, cp1258 43 | * cp10000, cp10006, cp10007, cp10029, cp10079, cp10081 44 | * utf8, utf16, utf16le, utf16be, utf32, utf32le, utf32be 45 | 46 | ## Options 47 | 48 | * return: list, binary 49 | * error: strict, ignore, replace 50 | * replace: `non_neg_integer` 51 | * bom: `true`, `false` 52 | 53 | ## License 54 | 55 | Copyright (c) 2015 woxtu 56 | 57 | Licensed under the MIT license. 58 | -------------------------------------------------------------------------------- /lib/mbcs.ex: -------------------------------------------------------------------------------- 1 | defmodule Mbcs do 2 | @moduledoc """ 3 | Wrapper for erlang-mbcs. 4 | This module provides functions for character encoding conversion. 5 | See `https://code.google.com/p/erlang-mbcs/` for detail. 6 | 7 | ## Usage 8 | 9 | # Start mbcs server 10 | iex> Mbcs.start 11 | :ok 12 | 13 | # Convert UTF-8 to Shift_JIS 14 | iex> Mbcs.encode!("九条カレン", :cp932) 15 | <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>> 16 | 17 | # Convert Shift_JIS to UTF-8, and return as a list 18 | iex> Mbcs.decode!([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932, return: :list) 19 | [20061, 26465, 12459, 12524, 12531] 20 | 21 | ## Support encodings 22 | 23 | * `:cp037` 24 | * `:cp437` 25 | * `:cp500` 26 | * `:cp737`, `:cp775` 27 | * `:cp850`, `:cp852`, `:cp855`, `:cp857`, `:cp860`, `:cp861`, `:cp862`, `:cp863`, `:cp864`, `:cp865`, `:cp866`, `:cp869`, `:cp874`, `:cp875` 28 | * `:cp932`, `:cp936`, `:gbk`, `:cp949`, `:cp950`, `:big5` 29 | * `:cp1026`, `:cp1250`, `:cp1251`, `:cp1252`, `:cp1253`, `:cp1254`, `:cp1255`, `:cp1256`, `:cp1257`, `:cp1258` 30 | * `:cp10000`, `:cp10006`, `:cp10007`, `:cp10029`, `:cp10079`, `:cp10081` 31 | * `:utf8`, `:utf16`, `:utf16le`, `:utf16be`, `:utf32`, `:utf32le`, `:utf32be` 32 | 33 | ## Options 34 | 35 | * return: `:list`, `:binary` 36 | * error: `:strict`, `:ignore`, `:replace` 37 | * replace: `non_neg_integer` 38 | * bom: `true`, `false` 39 | 40 | ## License 41 | Copyright (c) 2014 woxtu 42 | 43 | Licensed under the Boost Software License, Version 1.0. 44 | """ 45 | 46 | def start do 47 | :mbcs.start 48 | end 49 | 50 | def stop do 51 | :mbcs.stop 52 | end 53 | 54 | def encode(string, to, options \\ []) 55 | 56 | def encode(string, to, options) when is_bitstring(string) do 57 | to_list = if String.valid?(string), do: &to_charlist/1, else: &:erlang.bitstring_to_list/1 58 | 59 | encode(to_list.(string), to, options) 60 | end 61 | 62 | def encode(string, to, options) when is_list(string) do 63 | case :mbcs.encode(string, to, options) do 64 | {:error, reason} -> {:error, reason} 65 | result -> {:ok, result} 66 | end 67 | end 68 | 69 | def encode!(string, to, options \\ []) 70 | 71 | def encode!(string, to, options) when is_bitstring(string) do 72 | to_list = if String.valid?(string), do: &to_charlist/1, else: &:erlang.bitstring_to_list/1 73 | 74 | encode!(to_list.(string), to, options) 75 | end 76 | 77 | def encode!(string, to, options) when is_list(string) do 78 | case :mbcs.encode(string, to, options) do 79 | {:error, reason} -> raise to_error(reason) 80 | result -> result 81 | end 82 | end 83 | 84 | def decode(string, from, options \\ []) do 85 | case :mbcs.decode(string, from, options) do 86 | {:error, reason} -> {:error, reason} 87 | result -> if options[:return] == :list, do: {:ok, result}, else: {:ok, List.to_string(result)} 88 | end 89 | end 90 | 91 | def decode!(string, from, options \\ []) do 92 | case :mbcs.decode(string, from, options) do 93 | {:error, reason} -> raise to_error(reason) 94 | result -> if options[:return] == :list, do: result, else: List.to_string(result) 95 | end 96 | end 97 | 98 | def from_to(string, from, to, options \\ []) do 99 | case :mbcs.from_to(string, from, to, options) do 100 | {:error, reason} -> {:error, reason} 101 | result -> {:ok, result} 102 | end 103 | end 104 | 105 | def from_to!(string, from, to, options \\ []) do 106 | case :mbcs.from_to(string, from, to, options) do 107 | {:error, reason} -> raise to_error(reason) 108 | result -> result 109 | end 110 | end 111 | 112 | defmodule UnknownOptionError, do: defexception [:message] 113 | defmodule UnknownEncodingError, do: defexception [:message] 114 | defmodule UnmappingUnicodeError, do: defexception [:message] 115 | defmodule IllegalListError, do: defexception [:message] 116 | defmodule UndefinedCharacterError, do: defexception [:message] 117 | defmodule UnmappingCharacterError, do: defexception [:message] 118 | defmodule IncompleteMultibyteSequenceError, do: defexception [:message] 119 | defmodule UnmappingMultibyteCharacterError, do: defexception [:message] 120 | defmodule UnknownError, do: defexception [:message] 121 | 122 | defp to_error({:unknown_option, [option: option]}) do 123 | UnknownOptionError.exception(message: "option #{inspect option}") 124 | end 125 | 126 | defp to_error({:unkonwn_encoding, [encoding: encoding]}) do 127 | UnknownEncodingError.exception(message: "encoding #{inspect encoding}") 128 | end 129 | 130 | defp to_error({:unmapping_unicode, [unicode: code, pos: pos]}) do 131 | UnmappingUnicodeError.exception(message: "code #{code} in position #{pos}") 132 | end 133 | 134 | defp to_error({:illegal_list, [list: list, line: line]}) do 135 | IllegalListError.exception(message: "list #{inspect list} at line #{line}") 136 | end 137 | 138 | defp to_error({:undefined_character, [character: character, pos: pos]}) do 139 | UndefinedCharacterError.exception(message: "character #{character} in position #{pos}") 140 | end 141 | 142 | defp to_error({:unmapping_character, [character: character, pos: pos]}) do 143 | UnmappingCharacterError.exception(message: "character #{character} in position #{pos}") 144 | end 145 | 146 | defp to_error({:incomplete_multibyte_sequence, [leadbyte: leadbyte, pos: pos]}) do 147 | IncompleteMultibyteSequenceError.exception(message: "leadbyte #{leadbyte} in position #{pos}") 148 | end 149 | 150 | defp to_error({:unmapping_multibyte_character, [multibyte_character: character, pos: pos]}) do 151 | UnmappingMultibyteCharacterError.exception(message: "character #{character} in position #{pos}") 152 | end 153 | 154 | defp to_error(reason) do 155 | UnknownError.exception(message: inspect reason) 156 | end 157 | end 158 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule Mbcs.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [ app: :elixir_mbcs, 6 | version: "0.1.3", 7 | elixir: "~> 1.0", 8 | description: "Convert the character encoding", 9 | package: [ 10 | contributors: ["woxtu"], 11 | licenses: ["MIT"], 12 | links: %{"GitHub" => "https://github.com/woxtu/elixir-mbcs"} 13 | ], 14 | deps: deps() 15 | ] 16 | end 17 | 18 | def application do 19 | [] 20 | end 21 | 22 | defp deps do 23 | [{:mbcs, github: "nekova/erlang-mbcs"}] 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /test/mbcs_test.exs: -------------------------------------------------------------------------------- 1 | defmodule MbcsTest do 2 | use ExUnit.Case 3 | 4 | test "encode/2" do 5 | assert Mbcs.encode('九条カレン', :cp932) == {:ok, <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>} 6 | assert Mbcs.encode("九条カレン", :cp932) == {:ok, <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>} 7 | 8 | assert Mbcs.encode(<<255>>, :cp932) == {:error, {:unmapping_unicode, [unicode: 255, pos: 1]}} 9 | end 10 | 11 | test "encode/3" do 12 | assert Mbcs.encode('九条カレン', :cp932, return: :list) == {:ok, [139, 227, 143, 240, 131, 74, 131, 140, 131, 147]} 13 | assert Mbcs.encode('九条カレン', :cp932, return: :binary) == {:ok, <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>} 14 | assert Mbcs.encode("九条カレン", :cp932, return: :list) == {:ok, [139, 227, 143, 240, 131, 74, 131, 140, 131, 147]} 15 | assert Mbcs.encode("九条カレン", :cp932, return: :binary) == {:ok, <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>} 16 | 17 | assert Mbcs.encode(<<255>>, :cp932, error: :strict) == {:error, {:unmapping_unicode, [unicode: 255, pos: 1]}} 18 | assert Mbcs.encode(<<255>>, :cp932, error: :ignore) == {:ok, ""} 19 | end 20 | 21 | test "encode!/2" do 22 | assert Mbcs.encode!('九条カレン', :cp932) == <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>> 23 | assert Mbcs.encode!("九条カレン", :cp932) == <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>> 24 | 25 | assert_raise Mbcs.UnmappingUnicodeError, fn -> 26 | Mbcs.encode!(<<255>>, :cp932) 27 | end 28 | end 29 | 30 | test "encode!/3" do 31 | assert Mbcs.encode!('九条カレン', :cp932, return: :list) == [139, 227, 143, 240, 131, 74, 131, 140, 131, 147] 32 | assert Mbcs.encode!('九条カレン', :cp932, return: :binary) == <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>> 33 | assert Mbcs.encode!("九条カレン", :cp932, return: :list) == [139, 227, 143, 240, 131, 74, 131, 140, 131, 147] 34 | assert Mbcs.encode!("九条カレン", :cp932, return: :binary) == <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>> 35 | 36 | assert_raise Mbcs.UnmappingUnicodeError, fn -> 37 | Mbcs.encode!(<<255>>, :cp932, error: :strict) 38 | end 39 | assert Mbcs.encode!(<<255>>, :cp932, error: :ignore) == "" 40 | end 41 | 42 | test "decode/2" do 43 | assert Mbcs.decode([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932) == {:ok, "九条カレン"} 44 | assert Mbcs.decode(<<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>, :cp932) == {:ok, "九条カレン"} 45 | 46 | assert Mbcs.decode([255], :cp932) == {:error, {:undefined_character, [character: 255, pos: 1]}} 47 | assert Mbcs.decode(<<255>>, :cp932) == {:error, {:undefined_character, [character: 255, pos: 1]}} 48 | end 49 | 50 | test "decode/3" do 51 | assert Mbcs.decode([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932, return: :list) == {:ok, '九条カレン'} 52 | assert Mbcs.decode([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932, return: :binary) == {:ok, "九条カレン"} 53 | assert Mbcs.decode(<<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>, :cp932, return: :list) == {:ok, '九条カレン'} 54 | assert Mbcs.decode(<<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>, :cp932, return: :binary) == {:ok, "九条カレン"} 55 | 56 | assert Mbcs.decode([255], :cp932, error: :strict) == {:error, {:undefined_character, [character: 255, pos: 1]}} 57 | assert Mbcs.decode([255], :cp932, error: :ignore) == {:ok, ""} 58 | assert Mbcs.decode(<<255>>, :cp932, error: :strict) == {:error, {:undefined_character, [character: 255, pos: 1]}} 59 | assert Mbcs.decode(<<255>>, :cp932, error: :ignore) == {:ok, ""} 60 | end 61 | 62 | test "decode!/2" do 63 | assert Mbcs.decode!([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932) == "九条カレン" 64 | assert Mbcs.decode!(<<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>, :cp932) == "九条カレン" 65 | 66 | assert_raise Mbcs.UndefinedCharacterError, fn -> 67 | Mbcs.decode!([255], :cp932) 68 | end 69 | assert_raise Mbcs.UndefinedCharacterError, fn -> 70 | Mbcs.decode!(<<255>>, :cp932) 71 | end 72 | end 73 | 74 | test "decode!/3" do 75 | assert Mbcs.decode!([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932, return: :list) == '九条カレン' 76 | assert Mbcs.decode!([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932, return: :binary) == "九条カレン" 77 | assert Mbcs.decode!(<<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>, :cp932, return: :list) == '九条カレン' 78 | assert Mbcs.decode!(<<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>, :cp932, return: :binary) == "九条カレン" 79 | 80 | assert_raise Mbcs.UndefinedCharacterError, fn -> 81 | Mbcs.decode!([255], :cp932, error: :strict) 82 | end 83 | assert Mbcs.decode!([255], :cp932, error: :ignore) == "" 84 | assert_raise Mbcs.UndefinedCharacterError, fn -> 85 | Mbcs.decode!(<<255>>, :cp932, error: :strict) 86 | end 87 | assert Mbcs.decode!(<<255>>, :cp932, error: :ignore) == "" 88 | end 89 | 90 | test "from_to/3" do 91 | assert Mbcs.from_to([228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179], :utf8, :cp932) 92 | == {:ok, <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>} 93 | assert Mbcs.from_to([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932, :utf8) 94 | == {:ok, <<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>>} 95 | assert Mbcs.from_to(<<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>>, :utf8, :cp932) 96 | == {:ok, <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>} 97 | assert Mbcs.from_to(<<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>, :cp932, :utf8) 98 | == {:ok, <<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>>} 99 | 100 | assert Mbcs.from_to(<<255>>, :cp932, :utf8) == {:error, {:undefined_character, [character: 255, pos: 1]}} 101 | end 102 | 103 | test "from_to/4" do 104 | assert Mbcs.from_to([228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179], :utf8, :cp932, return: :list) 105 | == {:ok, [139, 227, 143, 240, 131, 74, 131, 140, 131, 147]} 106 | assert Mbcs.from_to([228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179], :utf8, :cp932, return: :binary) 107 | == {:ok, <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>} 108 | assert Mbcs.from_to([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932, :utf8, return: :list) 109 | == {:ok, [228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179]} 110 | assert Mbcs.from_to([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932, :utf8, return: :binary) 111 | == {:ok, <<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>>} 112 | assert Mbcs.from_to(<<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>>, :utf8, :cp932, return: :list) 113 | == {:ok, [139, 227, 143, 240, 131, 74, 131, 140, 131, 147]} 114 | assert Mbcs.from_to(<<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>>, :utf8, :cp932, return: :binary) 115 | == {:ok, <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>} 116 | assert Mbcs.from_to(<<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>, :cp932, :utf8, return: :list) 117 | == {:ok, [228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179]} 118 | assert Mbcs.from_to(<<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>, :cp932, :utf8, return: :binary) 119 | == {:ok, <<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>>} 120 | 121 | assert Mbcs.from_to(<<255>>, :cp932, :utf8, error: :strict) == {:error, {:undefined_character, [character: 255, pos: 1]}} 122 | assert Mbcs.from_to(<<255>>, :cp932, :utf8, error: :ignore) == {:ok, ""} 123 | end 124 | 125 | test "from_to!/3" do 126 | assert Mbcs.from_to!([228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179], :utf8, :cp932) 127 | == <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>> 128 | assert Mbcs.from_to!([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932, :utf8) 129 | == <<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>> 130 | assert Mbcs.from_to!(<<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>>, :utf8, :cp932) 131 | == <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>> 132 | assert Mbcs.from_to!(<<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>, :cp932, :utf8) 133 | == <<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>> 134 | 135 | assert_raise Mbcs.UndefinedCharacterError, fn -> 136 | Mbcs.from_to!(<<255>>, :cp932, :utf8) 137 | end 138 | end 139 | 140 | test "from_to!/4" do 141 | assert Mbcs.from_to!([228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179], :utf8, :cp932, return: :list) 142 | == [139, 227, 143, 240, 131, 74, 131, 140, 131, 147] 143 | assert Mbcs.from_to!([228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179], :utf8, :cp932, return: :binary) 144 | == <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>> 145 | assert Mbcs.from_to!([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932, :utf8, return: :list) 146 | == [228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179] 147 | assert Mbcs.from_to!([139, 227, 143, 240, 131, 74, 131, 140, 131, 147], :cp932, :utf8, return: :binary) 148 | == <<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>> 149 | assert Mbcs.from_to!(<<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>>, :utf8, :cp932, return: :list) 150 | == [139, 227, 143, 240, 131, 74, 131, 140, 131, 147] 151 | assert Mbcs.from_to!(<<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>>, :utf8, :cp932, return: :binary) 152 | == <<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>> 153 | assert Mbcs.from_to!(<<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>, :cp932, :utf8, return: :list) 154 | == [228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179] 155 | assert Mbcs.from_to!(<<139, 227, 143, 240, 131, 74, 131, 140, 131, 147>>, :cp932, :utf8, return: :binary) 156 | == <<228, 185, 157, 230, 157, 161, 227, 130, 171, 227, 131, 172, 227, 131, 179>> 157 | 158 | assert_raise Mbcs.UndefinedCharacterError, fn -> 159 | Mbcs.from_to!(<<255>>, :cp932, :utf8, error: :strict) 160 | end 161 | assert Mbcs.from_to!(<<255>>, :cp932, :utf8, error: :ignore) == "" 162 | end 163 | 164 | test "exceptions" do 165 | assert_raise Mbcs.UnknownOptionError, fn -> 166 | Mbcs.from_to!("", :utf8, :cp932, foo: :bar) 167 | end 168 | 169 | assert_raise Mbcs.UnknownEncodingError, fn -> 170 | Mbcs.from_to!("", :foo, :cp932) 171 | end 172 | end 173 | end 174 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | Mbcs.start 2 | ExUnit.start 3 | --------------------------------------------------------------------------------