| 1 |
/* |
| 2 |
* Copyright 2018 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "GlslMinify.h" |
| 18 |
|
| 19 |
#include <regex> |
| 20 |
|
| 21 |
namespace glslminifier { |
| 22 |
|
| 23 |
using std::regex; |
| 24 |
|
| 25 |
// Slash slash comments: |
| 26 |
// [\t ]* check for tabs and spaces up until |
| 27 |
// \/\/ two consecutive forward slashes |
| 28 |
// .* match the rest of the line |
| 29 |
const static regex slashSlashCommentsRegex(R"regex([\t ]*\/\/.*)regex"); |
| 30 |
|
| 31 |
// Star comments: |
| 32 |
// [\t ]* check for tabs and spaces up until |
| 33 |
// \/\* the characters '/' + '*' |
| 34 |
// [\w\W]*? match any character, including newlines. The ? makes it "non-greedy" |
| 35 |
// \*\/ stop the match at the first occurrence of '*' + '/' |
| 36 |
const static regex starCommentsRegex(R"regex([\t ]*\/\*[\w\W]*?\*\/)regex"); |
| 37 |
|
| 38 |
// Empty lines: |
| 39 |
// (\n|\r\n)+ match 1 or more newlines (either linux or Windows-style) |
| 40 |
// The capture group will only capture a single newline sequence. |
| 41 |
const static regex emptyLinesRegex(R"regex((\n|\r\n)+)regex"); |
| 42 |
|
| 43 |
// Indentation: |
| 44 |
// (^|\n) match the beginning of the file, or a newline character (C++14 does not support |
| 45 |
// the multiline option, unfortunately) |
| 46 |
// [ \t]+ match at least one leading space or tab character |
| 47 |
const static regex indentationRegex(R"regex((^|\n)[ \t]+)regex"); |
| 48 |
|
| 49 |
std::string minifyGlsl(const std::string& glsl, GlslMinifyOptions options) noexcept { |
| 50 |
std::string minified = glsl; |
| 51 |
|
| 52 |
if (options & GlslMinifyOptions::STRIP_COMMENTS) { |
| 53 |
minified = regex_replace(minified, slashSlashCommentsRegex, ""); |
| 54 |
minified = regex_replace(minified, starCommentsRegex, ""); |
| 55 |
} |
| 56 |
|
| 57 |
if (options & GlslMinifyOptions::STRIP_EMPTY_LINES) { |
| 58 |
// Replace the group of newlines with a single newline, which is in the first capture group. |
| 59 |
minified = regex_replace(minified, emptyLinesRegex, "$1"); |
| 60 |
} |
| 61 |
|
| 62 |
if (options & GlslMinifyOptions::STRIP_INDENTATION) { |
| 63 |
// Replace the indentation with whatever was captured in the first capture group, which will |
| 64 |
// either be a newline or nothing at all (in the case of the first line). |
| 65 |
// We do this to preserve newlines when stripping indentation. |
| 66 |
minified = regex_replace(minified, indentationRegex, "$1"); |
| 67 |
} |
| 68 |
|
| 69 |
return minified; |
| 70 |
} |
| 71 |
|
| 72 |
} // namespace glslminifier |