December 10, 2021
On Friday, 10 December 2021 at 13:22:58 UTC, Matheus wrote:

> 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;
> }


Oooh, finally someone suggested to preallocate storage for all these reinventions of the wheel :D

I would suggest instead of the final idup checking the length and only duplicating if certain waste threshold is broken, otherwise just doing https://dlang.org/phobos/std_exception.html#assumeUnique (or a cast to string). The result is unique either way.

Threshold could be relative for short strings and absolute for long ones. Makes little sense reallocating if you only waste a couple bytes, but makes perfect sense if you've just removed pages and pages of semicolons ;)

Be interesting to see if this thread does evolve into a SIMD search...
December 10, 2021
On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov wrote:
>
> Be interesting to see if this thread does evolve into a SIMD


http://lemire.me/blog/2017/01/20/how-quickly-can-you-remove-spaces-from-a-string/
December 10, 2021
On Friday, 10 December 2021 at 12:15:18 UTC, Rumbu wrote:
>
> I thought it's a beauty contest.
>

Well, if it's a beauty contest, then i got a beauty..

char[("abc;def;ab".length - count("abc;def;ab", ";"))] b = "abc;def;ab".replace(";", "");

December 10, 2021

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

>

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();

:)

"abc;def;ghi".tr(";", "", "d" );
December 10, 2021

On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov wrote:

>

Oooh, finally someone suggested to preallocate storage for all these reinventions of the wheel :D

import std.stdio;

char[] dontdothis(string s, int i=0, int skip=0){
    if (s.length == i) return new char[](i - skip);
    if (s[i] == ';') return dontdothis(s, i+1, skip+1);
    auto r = dontdothis(s, i+1, skip);
    r[i-skip] = s[i];
    return r;
}

int main() {
    string s = "abc;def;ab";
    string s_new = cast(string)dontdothis(s);
    writeln(s_new);
    return 0;
}
December 11, 2021
On Friday, 10 December 2021 at 22:35:58 UTC, Arjan wrote:
>
> "abc;def;ghi".tr(";", "", "d" );
>

I don't think we have enough ways of doing the same thing yet...

so here's one more..

"abc;def;ghi".substitute(";", "");

December 11, 2021

On Saturday, 11 December 2021 at 00:39:15 UTC, forkit wrote:

>

On Friday, 10 December 2021 at 22:35:58 UTC, Arjan wrote:

>

"abc;def;ghi".tr(";", "", "d" );

I don't think we have enough ways of doing the same thing yet...

so here's one more..

"abc;def;ghi".substitute(";", "");

Using libraries can trigger hidden allocations.

import std.stdio;

string garbagefountain(string s){
    if (s.length == 1) return s == ";" ? "" : s;
    return garbagefountain(s[0..$/2]) ~ garbagefountain(s[$/2..$]);
}

int main() {
    writeln(garbagefountain("abc;def;ab"));
    return 0;
}

December 11, 2021

On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov wrote:

>

Threshold could be relative for short strings and absolute for long ones. Makes little sense reallocating if you only waste a couple bytes, but makes perfect sense if you've just removed pages and pages of semicolons ;)

Scanning short strings twice is not all that expensive as they will stay in the CPU cache when you run over them a second time.

import std.stdio;

@safe:

string stripsemicolons(string s) @trusted {
    int i,n;
    foreach(c; s) n += c != ';'; // premature optimization
    auto r = new char[](n);
    foreach(c; s) if (c != ';') r[i++] = c;
    return cast(string)r;
}

int main() {
	writeln(stripsemicolons("abc;def;ab"));
    return 0;
}
December 11, 2021
On Saturday, 11 December 2021 at 08:05:01 UTC, Ola Fosheim Grøstad wrote:
>
> Using libraries can trigger hidden allocations.

ok. fine. no unnecessary, hidden allocations then.

// ------------------

module test;

import core.stdc.stdio : putchar;

nothrow @nogc void main()
{
    string str = "abc;def;ab";

    ulong len = str.length;

    for (ulong i = 0; i < len; i++)
    {
        if (cast(int) str[i] != ';')
            putchar(cast(int) str[i]);
    }
}

// ------------------

December 11, 2021

On Saturday, 11 December 2021 at 08:46:32 UTC, forkit wrote:

>

On Saturday, 11 December 2021 at 08:05:01 UTC, Ola Fosheim Grøstad wrote:

>

Using libraries can trigger hidden allocations.

ok. fine. no unnecessary, hidden allocations then.

// ------------------

module test;

import core.stdc.stdio : putchar;

nothrow @nogc void main()
{
string str = "abc;def;ab";

ulong len = str.length;

for (ulong i = 0; i < len; i++)
{
    if (cast(int) str[i] != ';')
        putchar(cast(int) str[i]);
}

}

// ------------------

putchar(…) is too slow!


@safe:

extern (C) long write(long, const void *, long);


void donttrythisathome(string s, char stripchar) @trusted {
	import core.stdc.stdlib;
    char* begin = cast(char*)alloca(s.length);
    char* end = begin;
    foreach(c; s) if (c != stripchar) *(end++) = c;
    write(0, begin, end - begin);
}


@system
void main() {
    string str = "abc;def;ab";
    donttrythisathome(str, ';');
}