Structure initializer VS lambda function

realhet real_het at hotmail.com
Sun Feb 19 10:24:05 UTC 2023


Hi again and thanks for the suggestions.

I ended up checking every {} block with the following program:
It works on a string where all the nested blocks are reduced to a 
single symbol. For example: '{', '"', '['
And all the comments and whitespaces are reduced to ' ' space.

```
enum CurlyBlockKind { empty, declarationsOrStatements, list }

auto detectCurlyBlock(CodeColumn col_)
{
     auto p = col_.extractThisLevelDString.text;
     p = p.replace("\n", " ");
     p = p.replace("  ", " ");
     p = p.replace(" {", "{");
     p = p.replace(" [", "]");
     p = p.replace(" (", ")");
     //opt: these replaces are slow.
     p = p.strip;

     //first start with easy decisions at the end of the block
     if(p=="") return CurlyBlockKind.empty;
     if(p.endsWith(';') || p.endsWith(':')) return 
CurlyBlockKind.declarationsOrStatements;

     if(p.canFind("{,") || p.canFind(",{")) return 
CurlyBlockKind.list;
     if(p.canFind(';')||p.canFind('{')) return 
CurlyBlockKind.declarationsOrStatements;

     //give it up: it's not a declaration, neither a statement 
block
     return CurlyBlockKind.list;
}
```

Since 2.5 months I didn't changed it, and I use it every day, so 
it seems ok.

The only unsure thing in this detector is the empty block. That 
would require to check what's around the empty block, but I just 
decided to represent it with it's own category: "empty".

The recursive detection of all D statements and declarations 
become easy:
- declarationsOrStatements -> Go inside this block and detect all 
the statements and declarations.
- list -> Discover the nested blocks inside this block, but don't 
treat this block as declarations or statements, this is a list!
- empty -> do nothing


More information about the Digitalmars-d-learn mailing list