[erlang-questions] Charset conversion / stylistic question.

Tim Becker tim.becker@REDACTED
Tue Apr 17 22:03:01 CEST 2007


Hi all,

I'm just starting out and still a little bit intimidated about how to
do things correctly in erlang and would like some advice. (In other
words, please bear with me.) I'd like to write some routines to recode
charsets and I'm not sure what would be the best way to go about it.
What I've come up with so far is:

  map (Function, <<A:8, Rest/binary>>) -> list_to_binary([Function(A)|
map (Function, Rest)]);
  map (_Function, <<>>) -> <<>>.

  convert({ebcdic, iso_8859_1}, <<Binary/binary>>) -> map(fun
cp037_to_iso8859_1/1, Binary);
  (...)
  convert({From, To}, <<_Binary/binary>>) -> {unsupported_encoding, From, To}.

  cp037_to_iso8859_1 (0) -> 0;
  (...)
  cp037_to_iso8859_1 (129) -> 97; % a
  cp037_to_iso8859_1 (130) -> 98; % b
  cp037_to_iso8859_1 (131) -> 99; % c
  cp037_to_iso8859_1 (132) -> 100; % d
  cp037_to_iso8859_1 (133) -> 101; % e
  (...)
  cp037_to_iso8859_1 (255) -> 159.


which works, so I'm relieved. But I'm not sure about the most
appropriate way to write the actual conversion functions. Using
pattern matching for each conversion like above, or just having one
function body with a bunch of if's in it or a case statement...

>From the languages I'm used to, I'd probably use a bunch of arrays and
have the value of the `from` charset be an index into the conversion
array. This would be possible as well, though it wouldn't be possible
to convert multibyte charsets like UTF-8...

Thanks in advance and sorry about the newbie question,
   -tim



More information about the erlang-questions mailing list