例子:
std::unordered_map<GLenum, std::string> OpenGLShader::PreProcess(const std::string& source) {
std::unordered_map<GLenum, std::string> shaderSources;
const char* typeToken = "#type";
size_t typeTockenLength = strlen(typeToken);
size_t pos = source.find(typeToken, 0);//find找不到会返回npos
while (pos != std::string::npos) {
size_t eol = source.find_first_of("\r\n", pos);
HZ_CORE_ASSERT(eol != std::string::npos, "Syntax error");
size_t begin = pos + typeTockenLength + 1;
std::string type = source.substr(begin, eol - begin);
HZ_CORE_ASSERT(ShaderTypeFromString(type), "Invalid shader type specified");
size_t nextLinePos = source.find_first_not_of("\r\n", eol);
pos = source.find(typeToken, nextLinePos);
shaderSources[ShaderTypeFromString(type)] = source.substr(nextLinePos,
pos - (nextLinePos == std::string::npos ? source.size() - 1 : nextLinePos));
}
return shaderSources;
}
找到pos位置之后第一个等于"\r\n"的字符位置:
size_t eol = source.find_first_of("\r\n", pos);
找到pos位置之后第一个不等于"\r\n"的字符位置:
size_t nextLinePos = source.find_first_not_of("\r\n", eol);

string::npos
1)可以表示string的结束位置
2)表示find()查找失败
本文详细介绍了如何使用C++标准库中的unordered_map和字符串操作函数,实现OpenGL着色器源代码的预处理,包括类型标记的查找、语法验证及源代码片段的提取。
8671

被折叠的 条评论
为什么被折叠?



