Erlang Strings - Which Libraries and Methods Should You Learn?

I am working on a project that will require internationalization support. I want to get started on the right leg with UTF support and I was wondering what is the best way to handle UTF in Erlang?

From my current research, it seems that there are several issues with Erlang's built in string handling for some use cases (good JSON parsing).

I was looking at Starling and recently read (somewhere) that it might be ported to Erlang standard edition as UTF standard. "Is this true? Are there other libraries or approaches I should look at?

From comments:

EEP (Erlang Improvement Proposal) 10 details Unicode character representation in Erlang

+2


source to share


2 answers


This page:

http://erlang.org/doc/highlights.html

... lists hightlights version 5.7 / OTP R13A. Note this passage:

1.2 Unicode support

Unicode support is implemented as described in EEP10. Formatting and reading data in Unicode from both terminals and files is supported by the io and io_lib modules. Files can be opened in modes with automatic translation from different Unicode. The 'unicode' module contains functions for converting between external and internal unicode formats and the re module is supported for Unicode data. There is also a language syntax for specifying strings and character data outside the ISO-latin-1 range.

I hate to make statements about what the best practices will be, but often I find it helpful to have a minimal, complete example to begin generalizing. Here's a way to get utf in an erlang app and send it again to a different context. Assuming you have a MySql database with a string field in a table containing utf8 characters, here's one way to get it and pass it to the web browser as json:

hg clone http://bitbucket.org/justin/webmachine/ webmachine-read-only
cd webmachine-read-only
make
./scripts/new_webmachine.erl mywebdemo /tmp
svn checkout http://erlang-mysql-driver.googlecode.com/svn/trunk/ erlang-mysql-driver-read-only
cd erlang-mysql-driver-read-only/src
cp * /tmp/mywebdemo/src
svn checkout http://mochiweb.googlecode.com/svn/trunk/ mochiweb-read-only
cp mochiweb-read-only/src/mochijson2.erl /tmp/mywebdemo/src
cd /tmp/mywebdemo

      



Modify src / mywebdemo_resource.erl so that it looks like this:

-module(mywebdemo_resource).
-export([init/1, to_html/2]). 

-include_lib("webmachine/include/webmachine.hrl").

init([]) -> {ok, undefined}.

to_html(ReqData, State) ->
    mysql:start_link(pool_id, "database.host.com", 3306, "db_user", "db_password", "db_name", fun(A, B, C, D) -> ouch end, utf8), %% add your connection string info
    {data, Res} = mysql:fetch(pool_id, "select * from table where IdWhatever = 13"),
    [[_, Utf8Str, _]] = mysql:get_result_rows(Res), %% pattern will need to be altered to match your table structure
    {mochijson2:encode({struct, [{Utf8Str, 100}]}), ReqData, State}.

      

Build everything and start the url manager:

make
./start.sh

      

Then follow these steps on the webpage (or something more convenient like MozRepl):

var req = new XMLHttpRequest;
req.open('GET', "http://localhost:8000", false);
req.send(null);
eval("(" + req.responseText + ")");

      

+5


source


As mentioned in the previous post, the latest version of erlang supports utf natively. If you can't use the latter, but then I usually use binaries for string data. It keeps erlang from manipulating the bytes in the list. It has the side effect of making it easier to process lists of strings.



0


source







All Articles