我试图创建一个函数,该函数能够将精度调制器关键字放在浮点变量前面,而没有这样的关键字,因此它将 const float x; 转换为 lowp const fl......
我试图创建一个能够
因此它转换
const float x;
到
lowp const float x;
但是我想忽略以下情况:
lowp const float x;
precision lowp float;
根据我之前的问题 链接 ,这是我的正则表达式:
(\bhighp((?:\s+\w+)*)(float|(?:i|b)?vec[2-4]|mat[2-4](?:x[2-4])?)(*SKIP)(?!)|\bfloat\b)
因此,我只是想忽略浮动之前存在 highp|lowp|mediump 的情况。
我有这个正则表达式命令:
#include <iostream>
#include <string>
#include <boost/regex/v5/regex.hpp>
using namespace boost;
std::string precisionModulation(std::string& shaderSource) {
const regex highpFloatRegex2(R"(highp|lowp|mediump((?:\s+\w+)*)(float)(*SKIP)(?!)|(?=\s+(float)))");
shaderSource = regex_replace(shaderSource, highpFloatRegex2, "lowp");
return shaderSource;
}
int main() {
std::string shaderSource = R"(
float foo1;
highp const float foo1;
precision highp float foo2;
)";
std::cout << "Original Shader Source:\n" << shaderSource << std::endl;
std::string modifiedSource = precisionModulation(shaderSource);
std::cout << "\nModified Shader Source:\n" << modifiedSource << std::endl;
return 0;
}
不幸的是我得到了奇怪的结果:
Original Shader Source:
float foo1;
highp const float foo1;
const highp float foo1;
precision highp float foo2;
Modified Shader Source:
lowp float foo1;
highp const lowp float foo1;
const highp lowp float foo1;
precision highp lowp float foo2;
我也尝试过:
\w*(?<!highp)((?:\s+\w+)*)\s+float
我建议使用
\bhighp(?:\s+\w+)*\s+(?:float|[ib]?vec[2-4]|mat[2-4](?:x[2-4])?)(*SKIP)(?!)|\b(?:const\s+)?float\b
查看 正则表达式演示 .
细节 :
\bhighp(?:\s+\w+)*\s+(?:float|[ib]?vec[2-4]|mat[2-4](?:x[2-4])?)(*SKIP)(?!)
- 匹配并跳过以下内容: \b
- word boundary highp
- a word highp
(?:\s+\w+)*
- zero or more occurrences of one or more whitespaces followed with one or more word chars \s+
- one or more whitespaces (in your original pattern, you are missing this pattern and it prevented from matching anything) (?:float|[ib]?vec[2-4]|mat[2-4](?:x[2-4])?)
- float
, or ivec
/ bvec
/ vec
followed with 2
, 3
or 4
, or mat
followed with 2
, 3
or 4
and then an optional x
followed with 2
, 3
or 4
|
- 或者
\b(?:const\s+)?float\b
- 整个单词 float
,其前面可以选择带有 const
一个或多个空格。
在您的代码中,它应该看起来像
const regex highpFloatRegex2(R"(\bhighp(?:\s+\w+)*\s+(?:float|[ib]?vec[2-4]|mat[2-4](?:x[2-4])?)(*SKIP)(?!)|\b(?:const\s+)?float\b)");
shaderSource = regex_replace(shaderSource, highpFloatRegex2, "lowp $0");
请注意, $0
替换模式中的代表整个匹配值。