toggleCase() for Narrow String

Salih Dincer salihdb at hotmail.com
Sun Apr 13 17:45:40 UTC 2025


On Sunday, 13 April 2025 at 17:28:12 UTC, Salih Dincer wrote:
>
> It's a very redundant thing (ASCII magic) that also weeds out 
> the incompatible character set.

Again and wgain opss! You can't get rid of if, but it's better 
with isAlpha(), like?

```d
auto toggleCase(T)(T str)
in (str.isAsciiString, "Incompatible character set!")
{
   import std.ascii : isAlpha;
   auto ret = str.dup;
   foreach (ref chr; ret)
   {
     if (chr.isAlpha)
     {
       chr ^= ' '; // toggle character
     }
   }
   return ret;
}

unittest
{
   string Aqw12 = `aQW
  12`;
   assert(Aqw12.toggleCase() == "Aqw\n 12");
}
```

SDB at 79




More information about the Digitalmars-d-learn mailing list