December 08, 2021

On Wednesday, 8 December 2021 at 22:55:02 UTC, forkit wrote:

>

On Wednesday, 8 December 2021 at 22:35:35 UTC, Stanislav Blinov wrote:

>

You're passing a literal. Try passing a runtime value (e.g. a command line argument). Also, -O2 -release :) Uless, of course, your goal is to look at debug code.

but this will change nothing.

the compilation cost of using .replace, will always be apparent (compared to the presented alternative), both in less time taken to compile, and smaller size of executable.

well... maybe not that apparent afterall ;-)

.. the mysteries of compiler optimisation ....

December 09, 2021

On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:

>

Let's say I want to skip characters and build a new string.

The string example to loop/iterate:

import std.stdio;

void main()
{
    string a="abc;def;ab";

}

The character I want to skip: ;

Expected result:

abcdefab

I always use split() and joiner pair. You can customize it as you want:

import std.stdio : writeln;
import std.algorithm : joiner;
import std.array : split;

bool isWhite(dchar c) @safe pure nothrow @nogc
{
  return c == ' ' || c == ';' ||
        (c >= 0x09&& c <= 0x0D);
}

void main()
{
    string str = "a\nb   c\t;d e f;a  b ";
    str.split!isWhite.joiner.writeln(); //abcdefab
}
December 09, 2021

On Wednesday, 8 December 2021 at 13:01:32 UTC, BoQsc wrote:
[...]

>

I'm not getting used to the syntax and that leads to poor readability.

It depends on what you expect when you read source code. I don't want to read how seats in the memory are assigned to bits and bytes. Instead I want to read what is done.

>

But that might be just me.

Unfortunately not.

>

Anyways,
Here is what I've come up with.

import std.stdio;

void main()
{
    string a = "abc;def;ab";
	string b;
	
	for(int i=0; i<a.length; i++){
		write(i);
		writeln(a[i]);
		if (a[i] != ';'){
			b ~= a[i];
		}
		
	}
	
    writeln(b);
}

PRO:

  • saves two lines of boilerplate code

CONS:

  • raw loop
  • postinc ++ is only permitted in ++C
  • inconsistent spacing around "="
  • mixing tabs and spaces for indentation
  • arrow code
December 09, 2021

On Wednesday, 8 December 2021 at 14:16:16 UTC, bauss wrote:
[...]

>

string b = a.replace(";", "");

👍

December 10, 2021
On Thursday, 9 December 2021 at 18:00:42 UTC, kdevel wrote:
>
> PRO:
>
> - saves two lines of boilerplate code
>
> CONS:
>
> - raw loop
> - postinc ++ is only permitted in ++C
> - inconsistent spacing around "="
> - mixing tabs and spaces for indentation
> - arrow code

more PROs:

 - You become less dependent on someone else's library.
 - You learn how to do some things yourself.

;-)

of course, I would prefer a less verbose, and safer version, which D enables, such as:

foreach(val; a)
    {
        writeln(val);
        if (val != ';')
        {
            b ~= val;
        }
    }


December 10, 2021

On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:

>

Let's say I want to skip characters and build a new string.
The character I want to skip: ;

Expected result:

abcdefab

Since it seems there is a contest here:

"abc;def;ghi".split(';').join();

:)

December 10, 2021

On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu wrote:

>

Since it seems there is a contest here:

"abc;def;ghi".split(';').join();

:)

Would that become two for loops or not?

December 10, 2021

On Friday, 10 December 2021 at 11:06:21 UTC, IGotD- wrote:

>

On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu wrote:

>

Since it seems there is a contest here:

"abc;def;ghi".split(';').join();

:)

Would that become two for loops or not?

I thought it's a beauty contest.

string stripsemicolons(string s)
{
  string result;
  // prevent reallocations
  result.length = s.length;
  result.length = 0;

  //append to string only when needed
  size_t i = 0;
  while (i < s.length)
  {
    size_t j = i;
    while (i < s.length && s[i] != ';')
      ++i;
    result ~= s[j..i];
  }
}
December 10, 2021
Yes it will. You can use lazy templates instead, like splitter and joiner, which splits and joins lazily, respectively. LDC can optimize those templates fairly well and avoid too much lazy calls and pretty much constructs the logic equivalent to for loop.

On 10 December 2021 11:06:21 WET, IGotD- via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
>On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu wrote:
>
>> 
>> Since it seems there is a contest here:
>> 
>> ```d
>> "abc;def;ghi".split(';').join();
>> ```
>> 
>> :)
>
>Would that become two for loops or not?


December 10, 2021
On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:
> ...
> The character I want to skip: `;`

My C way of thinking while using D:

import std;

string stripsemicolons(string input){
    char[] s = input.dup;
    int j=0;
    for(int i=0;i<input.length;++i){
        if(s[i] == ';'){ continue; }
        s[j++] = s[i];
    }
    s.length = j;
    return s.idup;
}

void main(){
    string s = ";testing;this;thing!;";
    writeln(s);
    writeln(s.stripsemicolons);
    return;
}

Matheus.