简体   繁体   中英

Pass n characters from string to function in C

I'm testing if a URL contains certain extensions. I have to do this about 100M times. I'm trying to pass the URL without the query string so I can compare the last 3 chars from the URL against some conditions.

My question is, can I pass only http://www.e.com/msusa/DisplayContactPage.jsp to textExtension ? without modifying url in main and without strdup the string?

int testExtension(char *url) {
    // compare last 3 chars against possible extensions
    // return 0 if matches extension, else return 1
    return match ? 0 : 1;
}

int main () {
    char *url = "http://www.e.com/msusa/DisplayContactPage.jsp?q=string&t=12"
    testExtension(url);
}

I can certainly do:

if ((end=strchr(url, '?'))) 
    *end=0;

but that modifies url

Steps you can take:

  1. Find the '?' in the URL.

     char* cp = strchr(url, '?'); 
  2. If you find it, move the pointer back by three. If you don't find it, move it to 3 characters before the end of the string.

    Check that the previous character is a '.' .That is the start of the extension. Pass the pointer to textExtension .

     if ( cp == NULL ) { len = strlen(url); cp = url + (len-3); } cp -= 3; if ( *(cp-1) != '.' ) { // Deal with the condition. } // Call textExtension. testExtension(cp); 
  3. Make sure you don't access anything beyond the '?' or the null character in testExtension .

If you are not sure about the number of characters in the extension, you can use:

char* cp = strchr(url, '?');
if ( cp == NULL )
{
   len = strlen(url);
   cp = url + len;
}

// Move the pointer back until you find the '.'
while ( *cp != '.' && cp > url )
{
   --cp;
}

There are a few ways to approach this.

Option 1: Operate on the substring

static const int EXTENSION_LEN = 3;

int testExtension(const char *url) {
    int pos = index(url, '?');
    if (pos > EXTENSION_LEN) {
        pos -= EXTENSION_LEN;
        return (0 == strncmp(EXTENSION, (url + pos), EXTENSION_LEN));
    }
    else {
        return 0;
    }
}

Depending on how many times you test the same URL, the overhead of that index() operation (linear in the length of the base URL) may become significant. You could avoid it by creating a copy of the extension (note that you don't need strdup() whole URL, but copy just the extension).

Option 2: Copy the substring to a new buffer

int testExtension(const char *extension) {
    return (0 == strncmp(EXTENSION, extension, EXTENSION_LEN));
}

int main() {
    char ext[EXTENSION_LEN];
    char *url = "http://www.e.com/msusa/DisplayContactPage.jsp?q=string&t=12";

    int testResult = 0;

    int pos = index(url, '?');
    if ( pos > EXTENSION_LEN ) {
        for ( int idx = 0; idx < EXTENSION_LEN; ++idx ) {
            ext[idx] = url[pos - EXTENSION_LEN + idx];
        }
        ext[EXTENSION_LEN - 1] = 0;  // null-terminate
        testResult = testExtension(ext);
    }
}

If you have a lot of extensions to test against, then a hash table or other data structure may be necessary to achieve decent performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM