Commit 8c5aeb6c by Jamie Madill

Add some string helper classes.

*re-land with build fix for Clang* These helper functions mirror some utilities in Chrome, for splitting strings along whitespace, or reading file contents into a string. Also remove the hack for skipping the doubly-defined GLX header. BUG=angleproject:892,angleproject:998 Change-Id: Ife43fbf5035a3be7820460bea1b26d0e632a4fb0 Reviewed-on: https://chromium-review.googlesource.com/272518Tested-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org>
parent 42c98f6b
//
// 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(inFile.tellg());
inFile.seekg(0, std::ios::beg);
str.assign(std::istreambuf_iterator<char>(inFile), std::istreambuf_iterator<char>());
return !inFile.fail();
}
}
//
// 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.
//
#ifndef LIBANGLE_STRING_UTILS_H_
#define LIBANGLE_STRING_UTILS_H_
#include <string>
#include <vector>
namespace angle
{
void SplitString(const std::string &input,
char delimiter,
std::vector<std::string> *tokensOut);
void SplitStringAlongWhitespace(const std::string &input,
std::vector<std::string> *tokensOut);
bool HexStringToUInt(const std::string &input, unsigned int *uintOut);
bool ReadFileToString(const std::string &path, std::string *stringOut);
}
#endif // LIBANGLE_STRING_UTILS_H_
//
// 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_unittests:
// Unit tests for the string utils.
//
#include "string_utils.h"
#include <gtest/gtest.h>
using namespace angle;
namespace
{
// Basic functionality tests for SplitString
TEST(StringUtilsTest, SplitStringBasic)
{
std::string testString("AxBxCxxxDExxFGHx");
std::vector<std::string> tokens;
SplitString(testString, 'x', &tokens);
ASSERT_EQ(5u, tokens.size());
EXPECT_EQ("A", tokens[0]);
EXPECT_EQ("B", tokens[1]);
EXPECT_EQ("C", tokens[2]);
EXPECT_EQ("DE", tokens[3]);
EXPECT_EQ("FGH", tokens[4]);
}
// Basic functionality tests for SplitStringAlongWhitespace
TEST(StringUtilsTest, SplitStringAlongWhitespaceBasic)
{
std::string testString("A B\nC\r\tDE\v\fFGH \t\r\n");
std::vector<std::string> tokens;
SplitStringAlongWhitespace(testString, &tokens);
ASSERT_EQ(5u, tokens.size());
EXPECT_EQ("A", tokens[0]);
EXPECT_EQ("B", tokens[1]);
EXPECT_EQ("C", tokens[2]);
EXPECT_EQ("DE", tokens[3]);
EXPECT_EQ("FGH", tokens[4]);
}
// Basic functionality tests for HexStringToUInt
TEST(StringUtilsTest, HexStringToUIntBasic)
{
std::string testStringA("0xBADF00D");
unsigned int uintValue;
ASSERT_TRUE(HexStringToUInt(testStringA, &uintValue));
EXPECT_EQ(0xBADF00Du, uintValue);
std::string testStringB("0xBADFOOD");
EXPECT_FALSE(HexStringToUInt(testStringB, &uintValue));
std::string testStringC("BADF00D");
EXPECT_FALSE(HexStringToUInt(testStringC, &uintValue));
}
// Note: ReadFileToString is harder to test
}
......@@ -10,6 +10,7 @@
#include <algorithm>
#include "common/string_utils.h"
#include "libANGLE/renderer/gl/renderergl_utils.h"
namespace rx
......@@ -779,7 +780,7 @@ void FunctionsGL::initialize()
else
{
const char *exts = reinterpret_cast<const char*>(getString(GL_EXTENSIONS));
extensions = TokenizeExtensionsString(exts);
angle::SplitStringAlongWhitespace(std::string(exts), &extensions);
}
// 1.0
......
......@@ -10,16 +10,14 @@
#include "libANGLE/renderer/gl/glx/FunctionsGLX.h"
#undef ANGLE_SKIP_GLX_DEFINES
// HACK(cwallez) this is a horrible hack to prevent glx from including GL/glext.h
// as it causes a bunch of conflicts (macro redefinition, etc) with GLES2/gl2ext.h
#define __glext_h_ 1
// We can only include glx.h in files which do not include ANGLE's GLES
// headers, to avoid doubly-defined GLenum macros, typedefs, etc.
#include <GL/glx.h>
#undef __glext_h_
#include <dlfcn.h>
#include <algorithm>
#include "libANGLE/renderer/gl/renderergl_utils.h"
#include "common/string_utils.h"
#include "libANGLE/renderer/gl/glx/functionsglx_typedefs.h"
namespace rx
......@@ -171,7 +169,7 @@ bool FunctionsGLX::initialize(Display *xDisplay, int screen, std::string *errorS
*errorString = "glXQueryExtensionsString returned NULL";
return false;
}
mExtensions = TokenizeExtensionsString(extensions);
angle::SplitStringAlongWhitespace(extensions, &mExtensions);
// GLX 1.3
GET_PROC_OR_ERROR(&mFnPtrs->getFBConfigsPtr, "glXGetFBConfigs");
......
......@@ -162,18 +162,4 @@ void GenerateCaps(const FunctionsGL *functions, gl::Caps *caps, gl::TextureCapsM
}
std::vector<std::string> TokenizeExtensionsString(const char *extensions)
{
std::vector<std::string> result;
std::istringstream stream(extensions);
std::string extension;
while (std::getline(stream, extension, ' '))
{
result.push_back(extension);
}
return result;
}
}
......@@ -33,7 +33,6 @@ void GenerateCaps(const FunctionsGL *functions, gl::Caps *caps, gl::TextureCapsM
gl::Extensions *extensions);
}
std::vector<std::string> TokenizeExtensionsString(const char *extensions);
}
#endif // LIBANGLE_RENDERER_GL_RENDERERGLUTILS_H_
......@@ -18,6 +18,8 @@
'common/mathutil.cpp',
'common/mathutil.h',
'common/platform.h',
'common/string_utils.cpp',
'common/string_utils.h',
'common/tls.cpp',
'common/tls.h',
'common/utilities.cpp',
......
......@@ -16,6 +16,7 @@
'angle_unittests_sources':
[
'<(angle_path)/src/common/Optional_unittest.cpp',
'<(angle_path)/src/common/string_utils_unittest.cpp',
'<(angle_path)/src/common/utilities_unittest.cpp',
'<(angle_path)/src/libANGLE/Config_unittest.cpp',
'<(angle_path)/src/libANGLE/Fence_unittest.cpp',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment