Search code examples
coptimizationscanfwhitespace

scanf() whitespace chars list


I'm trying to scan and parse strings like that:

var = val1/val2:mod1-mod2
var = val1/val2:mod1
var = val1/val2
var = val1
var

using this sscanf() expression:

vars = sscanf(instr, " %[^=\n\t ] = %[^/\n\t ] / %[^:\n\t ] : %[^-\n\t ] - %s", var, v1, v2, m1, m2);

My noob questions are:

  1. Is it possible somehow to specify a class here for whitespace chars, i.e. [:space:], in order not to specify them all?
  2. Any possible optimization of the regex?

Solution

  • You could do:

    #define WHITESPACE " \r\n\t\f\v"
    

    And then use string literal concatenation like so:

    vars = sscanf(
        instr,
        " %[^="WHITESPACE"] = %[^/"WHITESPACE"] / %[^:"WHITESPACE"] : %[^-"WHITESPACE"] - %s",
        var, v1, v2, m1, m2
    );
    

    It looks a bit ugly but it works. As for your format string itself, it looks fine as it is. Since it seems like you won't always find all 5 identifiers, checking the return value of sscanf will help you know how many matches were made, and it seems like you're already doing this.