How to avoid code duplication?

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 31 23:00:06 PDT 2015


On 1/04/2015 6:15 p.m., Suliman wrote:
>
> The situation is next:
>
> I have got the function that get arrays of lognames and logfullname:
>
> void loginsert(string [] lognames, string [] logfullname)
> {
>      if(logfullname[i].endsWith("txt"))
>      {
>          auto file = File(logfullname[i], "r");
>          foreach (line; file.byLine)
>          {
>              // long manupulation with strings
>          }
>      }
>
> }
>
> But now need add supporting passing to function not only txt files, but
> also zip archives.
> The problem that now I see only one way to do adding zip -- add block:
> if(logfullname[i].endsWith("zip"))
> And in it do everything with unpacking content of archive like:
>
>      foreach(ArchiveMember am; zip.directory)
>      {
>          string file = cast(string)zip.expand(am);
>          foreach (line; file.lineSplitter())
>              {
>
>              }
>      }
>
> and put inside it some same content that I am doing in "long
> manupulation with strings".
>
>
> Maybe there is any better way? But I really can't understand it because
> auto file = File(logfullname[i], "r"); can't read unpacked content of
> archive.

Use InputRange!string aka an input range which gives you elements. It 
can wrap up the behavior for getting the values.


More information about the Digitalmars-d-learn mailing list