www.digitalmars.com         C & C++   DMDScript  

D - Help: converting Performance code

reply "Andrew Edwards" <edwardsac spamfreeusa.com> writes:
The team of Koenig & Moo provided the following code in their column "Two
Kinds of Performance" in the October issue of CUJ. I'm wondering if someone
would be willing to help me convert it to D?

I looked at the documentation for Arrays, however, I was unable to find a D
equivalent for C++ iterators.

 Thanks a million.
Andrew

std::string deblank(std::string s)
{
  std::string::iterator in = s.begin(),
    out = in, end = s.end();
  while (in != end)
  {
    char c = *in;
    if (c != ' ')
    {
      *out = c;
      ++out;
    }
    ++in;
  }
  s.erase(out, end);
  return s;
}
Sep 17 2003
next sibling parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
Once again D proves itself to be very quick. Took me less than 5 mins to do
this!

// deblank.d

char[] deblank(char[] s)
{
    int     i = 0
        ,   o = 0;

    for(; i < s.length; ++i)
    {
        char    c   =   s[i];

        if(c != ' ')
        {
            s[o++] = s[i];
        }
    }
    s.length = o;

    return s;
}

// text_deblank_test.d

import <wherever you put it>.deblank;

int main(char[][] args)
{
    char[]  s1 = "String-#1";
    char[]  s2 = "  String-#2";
    char[]  s3 = "S t r i n g - # 3 ";

    printf("s1: \"%.*s\" => \"%.*s\"\n", s1, deblank(s1.dup));
    printf("s2: \"%.*s\" => \"%.*s\"\n", s2, deblank(s2.dup));
    printf("s3: \"%.*s\" => \"%.*s\"\n", s3, deblank(s3.dup));

    return 0;
}

// Results

H:\SynSoft\D\synsoft\text>text_deblank_test.exe
s1: "String-#1" => "String-#1"
s2: "  String-#2" => "String-#2"
s3: "S t r i n g - # 3 " => "String-#3"



"Andrew Edwards" <edwardsac spamfreeusa.com> wrote in message
news:bk92np$172b$1 digitaldaemon.com...
 The team of Koenig & Moo provided the following code in their column "Two
 Kinds of Performance" in the October issue of CUJ. I'm wondering if

 would be willing to help me convert it to D?

 I looked at the documentation for Arrays, however, I was unable to find a

 equivalent for C++ iterators.

  Thanks a million.
 Andrew

 std::string deblank(std::string s)
 {
   std::string::iterator in = s.begin(),
     out = in, end = s.end();
   while (in != end)
   {
     char c = *in;
     if (c != ' ')
     {
       *out = c;
       ++out;
     }
     ++in;
   }
   s.erase(out, end);
   return s;
 }

Sep 17 2003
parent reply "Walter" <walter digitalmars.com> writes:
While your example works, I wish to point out a style issue. It isn't
copy-on-write. General purpose functions that operate on arrays should dup
them if they modify the array. This avoids all kinds of ownership problems,
which your example deals with by having the caller do the dup rather than
the (I think properly) callee doing it.

"Matthew Wilson" <matthew stlsoft.org> wrote in message
news:bk941t$18q9$1 digitaldaemon.com...
 Once again D proves itself to be very quick. Took me less than 5 mins to

 this!

 // deblank.d

 char[] deblank(char[] s)
 {
     int     i = 0
         ,   o = 0;

     for(; i < s.length; ++i)
     {
         char    c   =   s[i];

         if(c != ' ')
         {
             s[o++] = s[i];
         }
     }
     s.length = o;

     return s;
 }

 // text_deblank_test.d

 import <wherever you put it>.deblank;

 int main(char[][] args)
 {
     char[]  s1 = "String-#1";
     char[]  s2 = "  String-#2";
     char[]  s3 = "S t r i n g - # 3 ";

     printf("s1: \"%.*s\" => \"%.*s\"\n", s1, deblank(s1.dup));
     printf("s2: \"%.*s\" => \"%.*s\"\n", s2, deblank(s2.dup));
     printf("s3: \"%.*s\" => \"%.*s\"\n", s3, deblank(s3.dup));

     return 0;
 }

 // Results

 H:\SynSoft\D\synsoft\text>text_deblank_test.exe
 s1: "String-#1" => "String-#1"
 s2: "  String-#2" => "String-#2"
 s3: "S t r i n g - # 3 " => "String-#3"



 "Andrew Edwards" <edwardsac spamfreeusa.com> wrote in message
 news:bk92np$172b$1 digitaldaemon.com...
 The team of Koenig & Moo provided the following code in their column


 Kinds of Performance" in the October issue of CUJ. I'm wondering if

 would be willing to help me convert it to D?

 I looked at the documentation for Arrays, however, I was unable to find


 D
 equivalent for C++ iterators.

  Thanks a million.
 Andrew

 std::string deblank(std::string s)
 {
   std::string::iterator in = s.begin(),
     out = in, end = s.end();
   while (in != end)
   {
     char c = *in;
     if (c != ' ')
     {
       *out = c;
       ++out;
     }
     ++in;
   }
   s.erase(out, end);
   return s;
 }


Sep 17 2003
parent "Matthew Wilson" <matthew stlsoft.org> writes:
He he. I was fully aware of them at the time, but just wanted to emulate the
original poster's sample code. :)

I agree wholeheartedly with the philosophy you're talking about - especially
since we don't have const, harrummpph - and think that's something we should
kind of get into as a D convention.

"Walter" <walter digitalmars.com> wrote in message
news:bkanun$hcs$1 digitaldaemon.com...
 While your example works, I wish to point out a style issue. It isn't
 copy-on-write. General purpose functions that operate on arrays should dup
 them if they modify the array. This avoids all kinds of ownership

 which your example deals with by having the caller do the dup rather than
 the (I think properly) callee doing it.

 "Matthew Wilson" <matthew stlsoft.org> wrote in message
 news:bk941t$18q9$1 digitaldaemon.com...
 Once again D proves itself to be very quick. Took me less than 5 mins to

 this!

 // deblank.d

 char[] deblank(char[] s)
 {
     int     i = 0
         ,   o = 0;

     for(; i < s.length; ++i)
     {
         char    c   =   s[i];

         if(c != ' ')
         {
             s[o++] = s[i];
         }
     }
     s.length = o;

     return s;
 }

 // text_deblank_test.d

 import <wherever you put it>.deblank;

 int main(char[][] args)
 {
     char[]  s1 = "String-#1";
     char[]  s2 = "  String-#2";
     char[]  s3 = "S t r i n g - # 3 ";

     printf("s1: \"%.*s\" => \"%.*s\"\n", s1, deblank(s1.dup));
     printf("s2: \"%.*s\" => \"%.*s\"\n", s2, deblank(s2.dup));
     printf("s3: \"%.*s\" => \"%.*s\"\n", s3, deblank(s3.dup));

     return 0;
 }

 // Results

 H:\SynSoft\D\synsoft\text>text_deblank_test.exe
 s1: "String-#1" => "String-#1"
 s2: "  String-#2" => "String-#2"
 s3: "S t r i n g - # 3 " => "String-#3"



 "Andrew Edwards" <edwardsac spamfreeusa.com> wrote in message
 news:bk92np$172b$1 digitaldaemon.com...
 The team of Koenig & Moo provided the following code in their column


 Kinds of Performance" in the October issue of CUJ. I'm wondering if

 would be willing to help me convert it to D?

 I looked at the documentation for Arrays, however, I was unable to



 a
 D
 equivalent for C++ iterators.

  Thanks a million.
 Andrew

 std::string deblank(std::string s)
 {
   std::string::iterator in = s.begin(),
     out = in, end = s.end();
   while (in != end)
   {
     char c = *in;
     if (c != ' ')
     {
       *out = c;
       ++out;
     }
     ++in;
   }
   s.erase(out, end);
   return s;
 }



Sep 17 2003
prev sibling next sibling parent "Walter" <walter digitalmars.com> writes:
char[] deblank(char[] s)
{  int i;
    char[] t;
    t.length = s.length;    // preallocate max possible size
    i = 0;
    foreach (char c; s)
   {
        if (c != ' ')
            t[i++] = c;
   }
   t.length = i;            // resize down to actual size
   return t;
}

"Andrew Edwards" <edwardsac spamfreeusa.com> wrote in message
news:bk92np$172b$1 digitaldaemon.com...
 The team of Koenig & Moo provided the following code in their column "Two
 Kinds of Performance" in the October issue of CUJ. I'm wondering if

 would be willing to help me convert it to D?

 I looked at the documentation for Arrays, however, I was unable to find a

 equivalent for C++ iterators.

  Thanks a million.
 Andrew

 std::string deblank(std::string s)
 {
   std::string::iterator in = s.begin(),
     out = in, end = s.end();
   while (in != end)
   {
     char c = *in;
     if (c != ' ')
     {
       *out = c;
       ++out;
     }
     ++in;
   }
   s.erase(out, end);
   return s;
 }

Sep 17 2003
prev sibling parent "Andrew Edwards" <edwardsac spamfreeusa.com> writes:
Gentlemen! Thanks for your assistance.

Andrew
Sep 17 2003