null == "" is true?

Ali Çehreli acehreli at yahoo.com
Tue Jul 12 18:35:58 UTC 2022


On 7/12/22 10:11, Steven Schveighoffer wrote:

 > The algorithm to compare *any* arrays is first verify the lengths are
 > the same. Then for each element in the array, compare them. Since there
 > are 0 elements in both the empty string and the null string, they are
 > equal.

Checking .empty() covered all of my uses cases. I think... :)

void foo(string s) {
   import std.array;
   assert(s.empty);
}

void main() {
   // Literal null
   foo(null);

   // Zero-length and pointing at '\0'
   foo("");

   // Fresh
   string a;
   foo(a);

   // Shrunk
   string b = "hello";
   b.length = 0;
   assert(b.ptr !is null);
   foo(b);
}

Ali



More information about the Digitalmars-d-learn mailing list