aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRory Byrne <[email protected]>2010-01-16 15:42:50 +0000
committerBjörn Gustavsson <[email protected]>2010-01-27 12:27:35 +0100
commitff65b85c9c81b9497a5e3342b9e995937cc286ee (patch)
treecf378cf669bf301e73d7f56d2e4bf5320300b3d7
parentcf7e585bb45970fe0b5a8a6aa6653cd50583d052 (diff)
downloadotp-ff65b85c9c81b9497a5e3342b9e995937cc286ee.tar.gz
otp-ff65b85c9c81b9497a5e3342b9e995937cc286ee.tar.bz2
otp-ff65b85c9c81b9497a5e3342b9e995937cc286ee.zip
Refactor out repeated block in re module
-rw-r--r--lib/stdlib/src/re.erl59
1 files changed, 11 insertions, 48 deletions
diff --git a/lib/stdlib/src/re.erl b/lib/stdlib/src/re.erl
index 889d273f6f..86da9a26f4 100644
--- a/lib/stdlib/src/re.erl
+++ b/lib/stdlib/src/re.erl
@@ -32,18 +32,7 @@ split(Subject,RE,Options) ->
try
{NewOpt,Convert,Unicode,Limit,Strip,Group} =
process_split_params(Options,iodata,false,-1,false,false),
- FlatSubject =
- case is_binary(Subject) of
- true ->
- Subject;
- false ->
- case Unicode of
- true ->
- unicode:characters_to_binary(Subject,unicode);
- false ->
- iolist_to_binary(Subject)
- end
- end,
+ FlatSubject = to_binary(Subject, Unicode),
case compile_split(RE,NewOpt) of
{error,_Err} ->
throw(badre);
@@ -217,30 +206,8 @@ replace(Subject,RE,Replacement,Options) ->
try
{NewOpt,Convert,Unicode} =
process_repl_params(Options,iodata,false),
- FlatSubject =
- case is_binary(Subject) of
- true ->
- Subject;
- false ->
- case Unicode of
- true ->
- unicode:characters_to_binary(Subject,unicode);
- false ->
- iolist_to_binary(Subject)
- end
- end,
- FlatReplacement =
- case is_binary(Replacement) of
- true ->
- Replacement;
- false ->
- case Unicode of
- true ->
- unicode:characters_to_binary(Replacement,unicode);
- false ->
- iolist_to_binary(Replacement)
- end
- end,
+ FlatSubject = to_binary(Subject, Unicode),
+ FlatReplacement = to_binary(Replacement, Unicode),
case do_replace(FlatSubject,Subject,RE,FlatReplacement,NewOpt) of
{error,_Err} ->
throw(badre);
@@ -634,18 +601,7 @@ grun(Subject,RE,{Options,NeedClean,OrigRE}) ->
grun2(Subject,RE,{Options,NeedClean}) ->
Unicode = check_for_unicode(RE,Options),
- FlatSubject =
- case is_binary(Subject) of
- true ->
- Subject;
- false ->
- case Unicode of
- true ->
- unicode:characters_to_binary(Subject,unicode);
- false ->
- iolist_to_binary(Subject)
- end
- end,
+ FlatSubject = to_binary(Subject, Unicode),
do_grun(FlatSubject,Subject,Unicode,RE,{Options,NeedClean}).
do_grun(FlatSubject,Subject,Unicode,RE,{Options0,NeedClean}) ->
@@ -765,3 +721,10 @@ runopt(global) ->
true;
runopt(_) ->
false.
+
+to_binary(Bin, _IsUnicode) when is_binary(Bin) ->
+ Bin;
+to_binary(Data, true) ->
+ unicode:characters_to_binary(Data,unicode);
+to_binary(Data, false) ->
+ iolist_to_binary(Data).