From 72713a28d5e1bcc22e4be3650504b7ea9dd90b29 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Wed, 5 Jun 2013 06:47:11 +0200
Subject: Clean up handling of extension addition groups

Break out the code to a separate function to make it more readable.

Also avoid hard-coding the name of the value to use as "Val1" as
it may not be true in the future.

Instead of using a list comprenhension like this:

  case [X || X <- [element(5, Val),element(6, Val)],
        X =/= asn1_NOVALUE] of
    [] -> ...;
    _ -> ...
  end

use an orelse chain:

  case element(5, Val) =/= asn1_NOVALUE orelse
       element(5, Val) =/= asn1_NOVALUE of
    false -> ...;
    true -> ...
  end
---
 lib/asn1/src/asn1ct_constructed_per.erl | 54 ++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 25 deletions(-)

(limited to 'lib/asn1/src')

diff --git a/lib/asn1/src/asn1ct_constructed_per.erl b/lib/asn1/src/asn1ct_constructed_per.erl
index 8eb6b91d01..463de965d7 100644
--- a/lib/asn1/src/asn1ct_constructed_per.erl
+++ b/lib/asn1/src/asn1ct_constructed_per.erl
@@ -97,31 +97,7 @@ gen_encode_constructed(Erule,Typename,D) when is_record(D,type) ->
     Ext = extensible_enc(CompList),
     case Ext of
 	{ext,_,NumExt} when NumExt > 0 ->
-	    case extgroup_pos_and_length(CompList) of
-		{extgrouppos,[]} -> % no extenstionAdditionGroup
-		    ok;
-		{extgrouppos,ExtGroupPosLenList} ->
-		    ExtGroupFun = 
-			fun({ExtActualGroupPos,ExtGroupVirtualPos,ExtGroupLen}) ->
-				Elements = 
-				    make_elements(ExtGroupVirtualPos+1,
-						  "Val1",
-						  lists:seq(1,ExtGroupLen)),
-				emit([
-				      {next,val}," = case [X || X <- [",Elements,
-				      "],X =/= asn1_NOVALUE] of",nl,
-				      "[] -> setelement(",
-				      {asis,ExtActualGroupPos+1},",",
-				      {curr,val},",",
-				      "asn1_NOVALUE);",nl,
-				      "_ -> setelement(",{asis,ExtActualGroupPos+1},",",
-				      {curr,val},",",
-				      "{extaddgroup,", Elements,"})",nl,
-				      "end,",nl]),
-				asn1ct_name:new(val)
-			end,
-		    lists:foreach(ExtGroupFun,ExtGroupPosLenList)
-	    end,
+	    gen_encode_extaddgroup(CompList),
 	    asn1ct_name:new(tmpval),
 	    emit(["Extensions = ",
 		  {call,Erule,fixextensions,[{asis,Ext},{curr,val}]},
@@ -194,6 +170,34 @@ gen_encode_constructed(Erule,Typename,D) when is_record(D,type) ->
     gen_enc_components_call(Erule,Typename,CompList,MaybeComma2,EncObj,Ext),
     emit({"].",nl}).
 
+gen_encode_extaddgroup(CompList) ->
+    case extgroup_pos_and_length(CompList) of
+	{extgrouppos,[]} ->
+	    ok;
+	{extgrouppos,ExtGroupPosLenList} ->
+	    _ = [do_gen_encode_extaddgroup(G) || G <- ExtGroupPosLenList],
+	    ok
+    end.
+
+do_gen_encode_extaddgroup({ActualGroupPos,GroupVirtualPos,GroupLen}) ->
+    Val = asn1ct_gen:mk_var(asn1ct_name:curr(val)),
+    Elements = make_elements(GroupVirtualPos+1,
+			     Val,
+			     lists:seq(1, GroupLen)),
+    Expr = any_non_value(GroupVirtualPos+1, Val, GroupLen, ""),
+    emit([{next,val}," = case ",Expr," of",nl,
+	  "false -> setelement(",{asis,ActualGroupPos+1},", ",
+	  {curr,val},", asn1_NOVALUE);",nl,
+	  "true -> setelement(",{asis,ActualGroupPos+1},", ",
+	  {curr,val},", {extaddgroup,", Elements,"})",nl,
+	  "end,",nl]),
+    asn1ct_name:new(val).
+
+any_non_value(_, _, 0, _) ->
+    [];
+any_non_value(Pos, Val, N, Sep) ->
+    Sep ++ [make_element(Pos, Val)," =/= asn1_NOVALUE"] ++
+	any_non_value(Pos+1, Val, N-1, [" orelse",nl]).
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %% generate decode function for SEQUENCE and SET
-- 
cgit v1.2.3