use & mask for array bounds clipping (provided array size is 2^n)

This commit is contained in:
Artem Pavlenko 2019-11-01 16:15:05 +00:00
parent dec6bc0950
commit 1edd3b7a93
2 changed files with 5 additions and 2 deletions

View file

@ -24,6 +24,9 @@
#include <unicode/uscript.h> #include <unicode/uscript.h>
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
const unsigned int STACK_SIZE = 1 << 7; // 2^n
const unsigned int STACK_MASK = STACK_SIZE - 1;
struct ScriptRecord struct ScriptRecord
{ {
UChar32 startChar = 0; UChar32 startChar = 0;
@ -85,7 +88,7 @@ private:
int32_t scriptEnd; int32_t scriptEnd;
UScriptCode scriptCode; UScriptCode scriptCode;
ParenStackEntry parenStack[128]; ParenStackEntry parenStack[STACK_SIZE];
int32_t parenSP; int32_t parenSP;
static int8_t highBit(int32_t value); static int8_t highBit(int32_t value);

View file

@ -161,7 +161,7 @@ UBool ScriptRun::next()
// characters above it on the stack will be poped. // characters above it on the stack will be poped.
if (pairIndex >= 0) { if (pairIndex >= 0) {
if ((pairIndex & 1) == 0) { if ((pairIndex & 1) == 0) {
parenSP = (++parenSP) % ARRAY_SIZE(parenStack); // avoid out-of-bounds access parenSP = (++parenSP) & STACK_MASK; // avoid out-of-bounds access
parenStack[parenSP].pairIndex = pairIndex; parenStack[parenSP].pairIndex = pairIndex;
parenStack[parenSP].scriptCode = scriptCode; parenStack[parenSP].scriptCode = scriptCode;
} else if (parenSP >= 0) { } else if (parenSP >= 0) {