yakra / DataProcessing

Data Processing Scripts and Programs for Travel Mapping Project
0 stars 0 forks source link

substr vs strcmp/strncmp #197

Closed yakra closed 2 years ago

yakra commented 2 years ago

https://github.com/yakra/DataProcessing/blob/79af5333b8ee089245e24365459c334e2451a168/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp#L443-L444

Neither strcmp nor strncmp work. Why exactly, again?

If switching over & adding the right qualifications is too much of a PitA, at least save the substring object & reuse.

yakra commented 2 years ago

If switching over & adding the right qualifications is too much of a PitA, at least save the substring object & reuse.

This happened: https://github.com/yakra/DataProcessing/blob/26855e2ce30837d2d675862851dbbba4be98066c/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp#L445-L446

That's cleaner; that's a start.

Neither strcmp nor strncmp work. Why exactly, again?

Not looking at the code & results (how did I try what again?); just doing this as a thought experiment:


More efficient to...

Checking for an underscore is unnecessary. Repeating the digit_starts & route comparisons is redundant. Instead, compare once and check for _ or \0. Adding 1 new line can get rid of 8 (or 9 if counting new bleeding-edge if (!colocated || !coloc_same_designation())) -- the first check expands to:

if (    !strncmp(slash+1, digit_starts, &*route->route.end() - digit_starts) && strchr("_", slash[&*route->route.end()-digit_starts+1])
     || !strncmp(slash+1, route->route.data(), route->route.size()) && strchr("_", slash[route->route.size()+1])
   ) {  if (!colocated || !coloc_same_designation())
      Datacheck::add(route, label, "", "", "LABEL_SELFREF", "");
    return;
     }

...A bit ugly though.


Even better?

Keep it relatively clean & readable:

if (!strdcmp(slash+1, digit_starts, '_') || !strdcmp(slash+1, route->route.data(), '_'))
{   if (!colocated || !coloc_same_designation())
      Datacheck::add(route, label, "", "", "LABEL_SELFREF", "");
    return;
}

A compact little function facilitates this:

inline int strdcmp(const char* a, const char* b, const char& d)
{   do  if (!*b) return *a == d ? 0 : *a-*b;
    while   (*a++ == *b++);
    return  *--a - *--b;
}

Advantages:

yakra commented 2 years ago

Tagged pending but not closed. Did I just forget about this, and it got addressed in https://github.com/TravelMapping/DataProcessing/pull/497? A reminder that that's what I wanna check out when I have the attention span.

yakra commented 2 years ago

This all got done. Seemed to forget about it in #497, only linking the Pending issues in TravelMapping/DataProcessing.