Edit

kc3-lang/angle/src/common/string_utils.cpp

Branch :

  • Show log

    Commit

  • Author : Jamie Madill
    Date : 2015-06-01 14:14:04
    Hash : c7be82ae
    Message : Change behaviour of string_utils, also fix file io. File IO tested manually in a follow-up patch. BUG=angleproject:998 Change-Id: I0bfa778d1787cdedfbf3cd68cd134477e6dcd23c Reviewed-on: https://chromium-review.googlesource.com/274030 Reviewed-by: Geoff Lang <geofflang@chromium.org> Tested-by: Jamie Madill <jmadill@chromium.org>

  • src/common/string_utils.cpp
  • //
    // Copyright 2015 The ANGLE Project Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style license that can be
    // found in the LICENSE file.
    //
    // string_utils:
    //   String helper functions.
    //
    
    #include "string_utils.h"
    
    #include <fstream>
    #include <sstream>
    
    namespace angle
    {
    
    void SplitString(const std::string &input,
                     char delimiter,
                     std::vector<std::string> *tokensOut)
    {
        std::istringstream stream(input);
        std::string token;
    
        while (std::getline(stream, token, delimiter))
        {
            if (!token.empty())
            {
                tokensOut->push_back(token);
            }
        }
    }
    
    void SplitStringAlongWhitespace(const std::string &input,
                                    std::vector<std::string> *tokensOut)
    {
        const char *delimiters = " \f\n\r\t\v";
    
        std::istringstream stream(input);
        std::string line;
    
        while (std::getline(stream, line))
        {
            size_t prev = 0, pos;
            while ((pos = line.find_first_of(delimiters, prev)) != std::string::npos)
            {
                if (pos > prev)
                    tokensOut->push_back(line.substr(prev, pos - prev));
                prev = pos + 1;
            }
            if (prev < line.length())
                tokensOut->push_back(line.substr(prev, std::string::npos));
        }
    }
    
    bool HexStringToUInt(const std::string &input, unsigned int *uintOut)
    {
        unsigned int offset = 0;
    
        if (input.size() >= 2 && input[0] == '0' && input[1] == 'x')
        {
            offset = 2u;
        }
    
        // Simple validity check
        if (input.find_first_not_of("0123456789ABCDEFabcdef", offset) != std::string::npos)
        {
            return false;
        }
    
        std::stringstream inStream(input);
        inStream >> std::hex >> *uintOut;
        return !inStream.fail();
    }
    
    bool ReadFileToString(const std::string &path, std::string *stringOut)
    {
        std::ifstream inFile(path.c_str());
        if (inFile.fail())
        {
            return false;
        }
    
        inFile.seekg(0, std::ios::end);
        stringOut->reserve(static_cast<std::string::size_type>(inFile.tellg()));
        inFile.seekg(0, std::ios::beg);
    
        stringOut->assign(std::istreambuf_iterator<char>(inFile), std::istreambuf_iterator<char>());
        return !inFile.fail();
    }
    
    }