Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:Ledest:erlang:21
eiconv
0002-Add-iconv-convert-3-API-2.patch
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 0002-Add-iconv-convert-3-API-2.patch of Package eiconv
From 3370698cd944acee41fd4e8c0dfc4035627df5c8 Mon Sep 17 00:00:00 2001 From: Yoan Tournade <yoan@ytotech.com> Date: Mon, 4 Sep 2017 08:35:31 +0200 Subject: [PATCH] Add iconv:convert/3 API (#2) * Add the iconv:convert/3 interface (returns directly the converted text instead of a tuple {ok, Text}) --- .editorconfig | 11 +++++++++++ README.md | 10 ++++++++++ src/iconv.erl | 26 +++++++++++++++++++------- test/iconv_test.erl | 12 ++++-------- 4 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..0e6c7af --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 4 diff --git a/README.md b/README.md index 31750d4..a74449d 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,16 @@ io:format("Converted '~s' to '~s'~n", [ToConvert, Converted]) ok = eiconvclose(CD) ``` +## `iconv` API + +### Using `convert/3` + +```erlang +Converted = eiconv:convert("utf-8", "ascii", "123")) +``` + +(Note it return directly the converted text and not a tuple {ok, Converted}) + ## Authors Wrapper provided by Maas-Maarten Zeeman and the [Zotonic](https://github.com/zotonic) team. diff --git a/src/iconv.erl b/src/iconv.erl index b5c79b6..1220470 100644 --- a/src/iconv.erl +++ b/src/iconv.erl @@ -3,27 +3,39 @@ % 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. -% drop in replacement module. +% drop in replacement module. -module(iconv). -author("Maas-Maarten Zeeman <mmzeeman@xs4all.nl>"). --export([open/2, conv/2, close/1]). +-export([open/2, conv/2, close/1, convert/3]). -open(ToCode, FromCode) -> +open(ToCode, FromCode) -> eiconv:open(ToCode, FromCode). -conv(Cd, Input) -> +conv(Cd, Input) -> eiconv:conv(Cd, Input). -close(Cd) -> +close(Cd) -> eiconv:close(Cd). + +% @doc Convert input which is in FromEncoding to ToEncoding. +% +convert(FromEncoding, ToEncoding, Input) -> + case eiconv:convert(FromEncoding, ToEncoding, Input) of + {ok, Output} -> Output; + {error, Error} -> + % throw the original error. + % TODO Let the badmatch occur? + % But it is not easy to catch by the user. + erlang:throw(Error) + end. diff --git a/test/iconv_test.erl b/test/iconv_test.erl index 469cc70..bf6b69b 100644 --- a/test/iconv_test.erl +++ b/test/iconv_test.erl @@ -21,7 +21,7 @@ ascii_to_utf8_test() -> iso8859_15_to_utf8_test() -> {ok, C} = iconv:open(<<"utf8">>, "iso8859-15"), ?assertEqual(<<194, 163, 226, 130, 172, 194, 165>>, conv(C, [163, 164, 165])). - + utf8_to_iso8859_15_test() -> {ok, C} = iconv:open("iso8859-15", "utf8"), ?assertEqual(<<163, 164, 165>>, conv(C, [194, 163, 226, 130, 172, 194, 165])), @@ -38,10 +38,6 @@ non_existing_codecs_test() -> ?assertEqual({error, einval}, iconv:open("utf-8", "iso8859-23")), ?assertEqual({error, einval}, iconv:open("iso8859-23", "utf-8")). - - - - - - - +convert_encoding_test() -> + ?assertException(throw, einval, iconv:convert("utf-8", "non-existent", "Test, 123")), + ?assertEqual(<<"123">>, iconv:convert("utf-8", "ascii", "123")). -- 2.16.4
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor