2010-12-04

Wanted: strpbrk()

I'm looking for a good EnScript API method for testing whether a string contains any characters in a second string.

That is, I want the standard C function strpbrk().

For some reason, I feel like earlier versions of EnCase had a function that did this. (By earliere versions, I mean v5 or v4.)

String::Trim() would almost do the job, except that it replaces the occurrences, which isn't what I want. The second parameter of Trim() is an int for passing in a combination of the String::TRIMOPTIONS enums. So, I tried passing in 0 instead of one of the enums, hoping this would find all occurrences but not replace them. Sadly, passing in 0 finds no occurrences.

You can use SearchClass with a character class regexp (e.g., [abc]) or NameListClass::Parse(), but both of them are pretty heavyweight.

In happier news, though, you can use EnScript to convert doubles to strings in scientific notation.

2 comments:

  1. Im not entirely sure what you mean. But could it be the Find() method you are looking for?

    ReplyDelete
  2. No, Find() looks for a substring/expression. What I want to know is whether a string contains any of a set of characters. For example, Windows forbids *, :, \, /, ", <, >, |, ? in file names, so it'd be nice to have a function where I could give it the "*:\\/\"<>|?" as a parameter and it would check whether any of them existed in the target string.

    A quick function to do this is:

    bool findChars(const String& s, const String& chars) {
    String copy = s;
    return copy.Trim(chars, String::TRIMALL) > 0;
    }

    This makes a copy (a malloc & one traversal of the string), then must traverse the copy looking for the characters. It may even make a second allocation and a third traversal. Still, it's probably less overhead then using SearchClass with a grep expression (e.g. "[\"\\/\|\*\?<>:]").

    ReplyDelete