Edit

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

Branch :

  • Show log

    Commit

  • Author : Corentin Wallez
    Date : 2015-05-21 10:51:01
    Hash : 7467b2b1
    Message : Fix warning in string_utils.cpp in Win32 BUG= Change-Id: Ibb25d46c2ee5f361f6a948dc3efea38422a4573f Reviewed-on: https://chromium-review.googlesource.com/272559 Reviewed-by: Jamie Madill <jmadill@chromium.org> Tested-by: Corentin Wallez <cwallez@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)
    {
        // Simple validity check
        if (input[0] != '0' || input[1] != 'x' ||
            input.find_first_not_of("0123456789ABCDEFabcdef", 2) != 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());
        std::string str;
    
        inFile.seekg(0, std::ios::end);
        str.reserve(static_cast<std::string::size_type>(inFile.tellg()));
        inFile.seekg(0, std::ios::beg);
    
        str.assign(std::istreambuf_iterator<char>(inFile), std::istreambuf_iterator<char>());
        return !inFile.fail();
    }
    
    }