// =============================================================== //
//                                                                 //
//   File      : arb_stdstr.h                                      //
//   Purpose   : inlined string functions using std::string        //
//                                                                 //
//   Coded by Ralf Westram (coder@reallysoft.de) in June 2002      //
//   Institute of Microbiology (Technical University Munich)       //
//   http://www.arb-home.de/                                       //
//                                                                 //
// =============================================================== //

// Note: code using char* should go to arb_str.h
//                          or ../CORE/arb_string.h

#ifndef ARB_STDSTR_H
#define ARB_STDSTR_H

#ifndef _GLIBCXX_STRING
#include <string>
#endif

inline bool beginsWith(const std::string& str, const std::string& start) {
    return str.find(start) == 0;
}

inline bool endsWith(const std::string& str, const std::string& postfix) {
    size_t slen = str.length();
    size_t plen = postfix.length();

    if (plen>slen) { return false; }
    return str.substr(slen-plen) == postfix;
}

class NoCaseCmp { // can be used as case insensitive key_compare for standard containers
    static bool less_nocase(char c1, char c2) { return toupper(c1) < toupper(c2); }
    static bool nonequal_nocase(char c1, char c2) { return toupper(c1) != toupper(c2); }
public:
    NoCaseCmp() {}

    bool operator()(const std::string& s1, const std::string& s2) const {
        return lexicographical_compare(s1.begin(), s1.end(),
                                       s2.begin(), s2.end(),
                                       less_nocase);
    }

    static bool has_prefix(const std::string& s1, const std::string& s2) {
        // return true if s1 starts with prefix s2
        return
            s1.length() >= s2.length() &&
            !lexicographical_compare(s1.begin(), s1.begin()+s2.length(),
                                     s2.begin(), s2.end(),
                                     nonequal_nocase);
    }
};


#else
#error arb_stdstr.h included twice
#endif // ARB_STDSTR_H
