Skip to content

substr vs strcmp/strncmp #197

Closed
Closed

Description

@yakra

&& (label.substr(slash-label.data()+1, underscore-slash-1) == route->route.data()+digit_starts+1
|| label.substr(slash-label.data()+1, underscore-slash-1) == route->route.data()))

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.

Activity

  1. added
    LABEL_SELFREF
    on Oct 31, 2021
  2. yakra commented on Nov 14, 2021

    @yakra
    OwnerAuthor

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

    This happened:

    { std::string postslash(slash+1, underscore-slash-1);
    if (postslash == digit_starts || postslash == route->route)

    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:

    • strcmp needs a null zero at the end of the search string; finds _ instead.
    • strncmp could find NY5 in NY50; need to check char after match.

    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:

    • Don't waste time checking within bounds of num on each iteration. Checking whether we're at the delimiter is one quick branch before returning.
    • Within Waypoint::label_selfref itself, avoid:
      • extra conditionals
      • the (slight) overhead of strchr
      • within strncmp calls:
        • indirection
        • function calls, however lightweight
        • pointer arithmetic
    • AW YISS.
  3. yakra commented on Feb 24, 2022

    @yakra
    OwnerAuthor

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

  4. yakra commented on Mar 20, 2022

    @yakra
    OwnerAuthor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C++LABEL_SELFREFdatacheckefficiencylow prioritypending

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions