[ create a new paste ] login | about

Link: http://codepad.org/uCWA3Ttd    [ raw code | output | fork ]

C++, pasted on Jun 26:
#include <string>
#include <cstring>
#include <cctype>
#include <iostream>
#include <stdexcept>

using namespace std;

enum ET_strippingMode {
    exclusive = 0,
    inclusive = 1
};

bool searchStringIsSpecialString(const char searchString[]) {
    return strcmp(searchString, "A-Za-z") == 0;
}

bool sourceStringContainsAlphabetChar(const char sourceString[]) {
    //we are assuming 'sourceString' is pointed at a null-terminated char array...
    unsigned int sourceStringSize = strlen(sourceString);

    for (unsigned int i = 0; i < sourceStringSize; i++) {
        if (isalpha(sourceString[i])) {
            return true; //at the first char in the range of A-Z or a-z
        }
    }

    return false;
}

bool containsString(const char sourceString[], const char searchString[]) {
    //assume that 'sourceString' and 'searchString' are pointing at two null-terminated char arrays.

    //if the special string "A-Za-z" was passed into the 'searchString' parameter
    //then search for a character in sourceString in the range of A-Z, or a-z.
    if (searchStringIsSpecialString(searchString) && sourceStringContainsAlphabetChar(sourceString)) {
        return true;
    }
    else {
        size_t sourceStringSize = strlen(sourceString);
        size_t searchStringSize = strlen(searchString);
        if (searchStringSize < sourceStringSize) {
            //check to see if the searchString is contained in the sourceString.
            return strstr(sourceString, searchString) != NULL;
        }
        else if (searchStringSize == sourceStringSize) {
            //check to see if the source string and the search string are the same.
            return strcmp(sourceString, searchString) == 0;
        }
        else {
            //there is no possibility of the shorter string fully containing the longer string.
            return false;
        }
    }
}

namespace
{
    string stripStringExclusive(const char str[], size_t size, const char end, size_t i) {
        string strippedString = "";
        for (; i < size && str[i] != end; ++i) {
            if (str[i] == end) {
                return strippedString;
            }
            else if (str[i] != ' ') { //skip spaces
                strippedString.push_back(str[i]);
            }
        }
        return ""; //end char is not present
    }

    string stripStringInclusive(const char str[], size_t size, const char end, size_t i) {
        string strippedString = "";
        for (; i < size && str[i] != end; ++i) {
            if (str[i] == end) {
                strippedString.push_back(end);
                return strippedString;
            }
            else if (str[i] != ' ') { //skip spaces
                strippedString.push_back(str[i]);
            }
        }
        return ""; //end char is not present
    }
}

string stripString(const char str[], const char start, const char end, ET_strippingMode strippingMode = exclusive) {
    char* start_ptr = strchr(str, start);
    if (start_ptr == NULL) {
        return false;
    }

    if (strippingMode == exclusive) {
        return stripStringExclusive(str, strlen(str), end, str - start_ptr + 1);
    }
    else if (strippingMode == inclusive) {
        return stripStringInclusive(str, strlen(str), end, str - start_ptr);
    }
    else {
        throw invalid_argument("stripping mode must be either exclusive or inclusive");
    }
}

int main() {
    char filestreamInBuffer[] = "enum DAY {";
    if (containsString(filestreamInBuffer, "enum")) {
        cout<< "Yes, \"" << filestreamInBuffer << "\" does indeed contain \"enum\"." <<endl;
    }
    else {
        cout<< "No, \"" << filestreamInBuffer << "\" does not contain \"enum\"." <<endl;
    }

    if (containsString(filestreamInBuffer, ";")) {
        cout<< "Yes, \"" << filestreamInBuffer << "\" does indeed contain \";\"." <<endl;
    }
    else {
        cout<< "No, \"" << filestreamInBuffer << "\" does not contain \";\"." <<endl;
    }

    string currentEnumName = stripString(filestreamInBuffer, 'm', '{');
    cout<< "The name of the current enum is: " << currentEnumName <<endl;

    return 0;
}


Output:
1
2
3
Yes, "enum DAY {" does indeed contain "enum".
No, "enum DAY {" does not contain ";".
The name of the current enum is: 


Create a new paste based on this one


Comments: