Skip to content

Commit

Permalink
made the style buffer store uint8_t's natively, which removes the nee…
Browse files Browse the repository at this point in the history
…d for a LOT of casts

also switched the nm pow function to use lround instead of doing it manually and clunkily
  • Loading branch information
eteran committed Apr 3, 2024
1 parent fac7e55 commit 992a70e
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 90 deletions.
14 changes: 7 additions & 7 deletions Interpreter/interpret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,7 @@ static int power() {
don't want to round this to 1. This is mainly intended to deal with
4^2 = 15.999996 and 16.000001.
*/

if (n2 < 0 && n1 != 1 && n1 != -1) {
if (n1 != 0) {
// since we're integer only, nearly all negative exponents result in 0
Expand All @@ -1553,14 +1554,13 @@ static int power() {
n3 = static_cast<int>(pow(static_cast<double>(n1), static_cast<double>(n2)));
}
} else {
if ((n1 < 0) && (n2 & 1)) {
// round to nearest integer for negative values
n3 = static_cast<int>(pow(static_cast<double>(n1), static_cast<double>(n2)) - 0.5);
} else {
// round to nearest integer for positive values
n3 = static_cast<int>(pow(static_cast<double>(n1), static_cast<double>(n2)) + 0.5);
}
// round to nearest integer
// NOTE(eteran): this use to detect if the result would be negative and round by adding/subtracting
// 0.5 before the final cast to int
// this SHOULD be the equivalent to that
n3 = static_cast<int>(std::lround(pow(static_cast<double>(n1), static_cast<double>(n2))));
}

PUSH_INT(n3);
return errCheck("exponentiation");
}
Expand Down
13 changes: 8 additions & 5 deletions Regex/Compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

namespace {

const auto FirstPassToken = reinterpret_cast<uint8_t *>(1);


// Flags for function shortcut_escape()
enum ShortcutEscapeFlag {
CHECK_ESCAPE = 0, // Check an escape sequence for validity only.
Expand Down Expand Up @@ -182,7 +185,7 @@ uint8_t *emit_node(T op_code) noexcept {

if (pContext.FirstPass) {
pContext.Reg_Size += NODE_SIZE<size_t>;
return reinterpret_cast<uint8_t *>(1);
return FirstPassToken;
}

const size_t end_offset = pContext.Code.size();
Expand Down Expand Up @@ -261,7 +264,7 @@ uint8_t *emit_special(Ch op_code, uint32_t test_val, size_t index) noexcept {
pContext.Reg_Size += NODE_SIZE<size_t>; // Make room for the node.
}

return reinterpret_cast<uint8_t *>(1);
return FirstPassToken;
}

uint8_t *ret_val = emit_node(op_code); // Return the address for start of node.
Expand Down Expand Up @@ -331,7 +334,7 @@ uint8_t *insert(uint8_t op, const uint8_t *insert_pos, uint32_t min, uint32_t ma
}

pContext.Reg_Size += insert_size;
return reinterpret_cast<uint8_t *>(1);
return FirstPassToken;
}

// Where operand used to be.
Expand Down Expand Up @@ -406,7 +409,7 @@ uint8_t *shortcut_escape(Ch ch, int *flag_param) {
static const char codes[] = "ByYdDlLsSwW";

const char *clazz = nullptr;
auto ret_val = reinterpret_cast<uint8_t *>(1); // Assume success.
auto ret_val = FirstPassToken; // Assume success.
const char *valid_codes;

if (Flags == EMIT_CLASS_BYTES || Flags == CHECK_CLASS_ESCAPE) {
Expand Down Expand Up @@ -611,7 +614,7 @@ uint8_t *back_ref(const char *ch, int *flag_param) {
*flag_param |= HAS_WIDTH;
}
} else if (Flags == CHECK_ESCAPE) {
ret_val = reinterpret_cast<uint8_t *>(1);
ret_val = FirstPassToken;
} else {
ret_val = nullptr;
}
Expand Down
58 changes: 29 additions & 29 deletions src/DocumentWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5685,12 +5685,12 @@ Style DocumentWidget::getHighlightInfo(TextCursor pos) {
}

// Be careful with signed/unsigned conversions. NO conversion here!
auto style = static_cast<uint8_t>(highlightData->styleBuffer->BufGetCharacter(pos));
uint8_t style = highlightData->styleBuffer->BufGetCharacter(pos);

// Beware of unparsed regions.
if (style == UNFINISHED_STYLE) {
handleUnparsedRegion(highlightData->styleBuffer, pos);
style = static_cast<uint8_t>(highlightData->styleBuffer->BufGetCharacter(pos));
style = highlightData->styleBuffer->BufGetCharacter(pos);
}

if (highlightData->pass1Patterns) {
Expand Down Expand Up @@ -5718,17 +5718,17 @@ int64_t DocumentWidget::styleLengthOfCodeFromPos(TextCursor pos) const {
const TextCursor oldPos = pos;

if (const std::unique_ptr<WindowHighlightData> &highlightData = highlightData_) {
if (const std::shared_ptr<TextBuffer> &styleBuf = highlightData->styleBuffer) {
if (const std::shared_ptr<UTextBuffer> &styleBuf = highlightData->styleBuffer) {

auto hCode = static_cast<uint8_t>(styleBuf->BufGetCharacter(pos));
uint8_t hCode = styleBuf->BufGetCharacter(pos);
if (!hCode) {
return 0;
}

if (hCode == UNFINISHED_STYLE) {
// encountered "unfinished" style, trigger parsing
handleUnparsedRegion(highlightData->styleBuffer, pos);
hCode = static_cast<uint8_t>(styleBuf->BufGetCharacter(pos));
hCode = styleBuf->BufGetCharacter(pos);
}

StyleTableEntry *entry = styleTableEntryOfCode(hCode);
Expand All @@ -5742,10 +5742,10 @@ int64_t DocumentWidget::styleLengthOfCodeFromPos(TextCursor pos) const {
if (hCode == UNFINISHED_STYLE) {
// encountered "unfinished" style, trigger parsing, then loop
handleUnparsedRegion(highlightData->styleBuffer, pos);
hCode = static_cast<uint8_t>(styleBuf->BufGetCharacter(pos));
hCode = styleBuf->BufGetCharacter(pos);
} else {
// advance the position and get the new code
hCode = static_cast<uint8_t>(styleBuf->BufGetCharacter(++pos));
hCode = styleBuf->BufGetCharacter(++pos);
}
}
}
Expand Down Expand Up @@ -5806,13 +5806,13 @@ size_t DocumentWidget::highlightCodeOfPos(TextCursor pos) const {
size_t hCode = 0;
if (const std::unique_ptr<WindowHighlightData> &highlightData = highlightData_) {

if (const std::shared_ptr<TextBuffer> &styleBuf = highlightData->styleBuffer) {
if (const std::shared_ptr<UTextBuffer> &styleBuf = highlightData->styleBuffer) {

hCode = static_cast<uint8_t>(styleBuf->BufGetCharacter(pos));
hCode = styleBuf->BufGetCharacter(pos);
if (hCode == UNFINISHED_STYLE) {
// encountered "unfinished" style, trigger parsing
handleUnparsedRegion(highlightData->styleBuffer, pos);
hCode = static_cast<uint8_t>(styleBuf->BufGetCharacter(pos));
hCode = styleBuf->BufGetCharacter(pos);
}
}
}
Expand All @@ -5831,17 +5831,17 @@ int64_t DocumentWidget::highlightLengthOfCodeFromPos(TextCursor pos) const {

if (const std::unique_ptr<WindowHighlightData> &highlightData = highlightData_) {

if (const std::shared_ptr<TextBuffer> &styleBuf = highlightData->styleBuffer) {
if (const std::shared_ptr<UTextBuffer> &styleBuf = highlightData->styleBuffer) {

auto hCode = static_cast<uint8_t>(styleBuf->BufGetCharacter(pos));
uint8_t hCode = styleBuf->BufGetCharacter(pos);
if (!hCode) {
return 0;
}

if (hCode == UNFINISHED_STYLE) {
// encountered "unfinished" style, trigger parsing
handleUnparsedRegion(highlightData->styleBuffer, pos);
hCode = static_cast<uint8_t>(styleBuf->BufGetCharacter(pos));
hCode = styleBuf->BufGetCharacter(pos);
}

if (checkCode == 0) {
Expand All @@ -5852,10 +5852,10 @@ int64_t DocumentWidget::highlightLengthOfCodeFromPos(TextCursor pos) const {
if (hCode == UNFINISHED_STYLE) {
// encountered "unfinished" style, trigger parsing, then loop
handleUnparsedRegion(highlightData->styleBuffer, pos);
hCode = static_cast<uint8_t>(styleBuf->BufGetCharacter(pos));
hCode = styleBuf->BufGetCharacter(pos);
} else {
// advance the position and get the new code
hCode = static_cast<uint8_t>(styleBuf->BufGetCharacter(++pos));
hCode = styleBuf->BufGetCharacter(++pos);
}
}
}
Expand All @@ -5873,7 +5873,7 @@ int64_t DocumentWidget::highlightLengthOfCodeFromPos(TextCursor pos) const {
** needs re-parsing. This routine applies pass 2 patterns to a chunk of
** the buffer of size PASS_2_REPARSE_CHUNK_SIZE beyond pos.
*/
void DocumentWidget::handleUnparsedRegion(TextBuffer *styleBuf, TextCursor pos) const {
void DocumentWidget::handleUnparsedRegion(UTextBuffer *styleBuf, TextCursor pos) const {
TextBuffer *buf = info_->buffer.get();
const std::unique_ptr<WindowHighlightData> &highlightData = highlightData_;

Expand All @@ -5896,8 +5896,8 @@ void DocumentWidget::handleUnparsedRegion(TextBuffer *styleBuf, TextCursor pos)
TextCursor beginSafety = Highlight::backwardOneContext(buf, context, beginParse);

for (TextCursor p = beginParse; p >= beginSafety; --p) {
char ch = styleBuf->BufGetCharacter(p);
if (ch != UNFINISHED_STYLE && ch != PLAIN_STYLE && static_cast<uint8_t>(ch) < firstPass2Style) {
uint8_t ch = styleBuf->BufGetCharacter(p);
if (ch != UNFINISHED_STYLE && ch != PLAIN_STYLE && ch < firstPass2Style) {
beginSafety = p + 1;
break;
}
Expand All @@ -5911,16 +5911,16 @@ void DocumentWidget::handleUnparsedRegion(TextBuffer *styleBuf, TextCursor pos)
TextCursor endSafety = Highlight::forwardOneContext(buf, context, endParse);

for (TextCursor p = pos; p < endSafety; ++p) {
char ch = styleBuf->BufGetCharacter(p);
if (ch != UNFINISHED_STYLE && ch != PLAIN_STYLE && static_cast<uint8_t>(ch) < firstPass2Style) {
uint8_t ch = styleBuf->BufGetCharacter(p);
if (ch != UNFINISHED_STYLE && ch != PLAIN_STYLE && ch < firstPass2Style) {
endParse = std::min(endParse, p);
endSafety = p;
break;
}

if (ch != UNFINISHED_STYLE && p < endParse) {
endParse = p;
if (static_cast<uint8_t>(ch) < firstPass2Style) {
if (ch < firstPass2Style) {
endSafety = p;
} else {
endSafety = Highlight::forwardOneContext(buf, context, endParse);
Expand All @@ -5933,9 +5933,9 @@ void DocumentWidget::handleUnparsedRegion(TextBuffer *styleBuf, TextCursor pos)
std::string str = buf->BufGetRange(beginSafety, endSafety);
const char *string = &str[0];

std::string styleStr = styleBuf->BufGetRange(beginSafety, endSafety);
char *const styleString = &styleStr[0];
char *stylePtr = &styleStr[0];
std::basic_string<uint8_t> styleStr = styleBuf->BufGetRange(beginSafety, endSafety);
uint8_t *const styleString = &styleStr[0];
uint8_t *stylePtr = &styleStr[0];

// Parse it with pass 2 patterns
int prev_char = Highlight::getPrevChar(buf, beginSafety);
Expand All @@ -5955,7 +5955,7 @@ void DocumentWidget::handleUnparsedRegion(TextBuffer *styleBuf, TextCursor pos)

/* Update the style buffer the new style information, but only between
beginParse and endParse. Skip the safety region */
auto view = view::string_view(&styleString[beginParse - beginSafety], static_cast<size_t>(endParse - beginParse));
auto view = UTextBuffer::view_type(&styleString[beginParse - beginSafety], static_cast<size_t>(endParse - beginParse));
styleBuf->BufReplace(beginParse, endParse, view);
}

Expand All @@ -5964,7 +5964,7 @@ void DocumentWidget::handleUnparsedRegion(TextBuffer *styleBuf, TextCursor pos)
* @param styleBuf
* @param pos
*/
void DocumentWidget::handleUnparsedRegion(const std::shared_ptr<TextBuffer> &styleBuf, TextCursor pos) const {
void DocumentWidget::handleUnparsedRegion(const std::shared_ptr<UTextBuffer> &styleBuf, TextCursor pos) const {
handleUnparsedRegion(styleBuf.get(), pos);
}

Expand Down Expand Up @@ -5995,9 +5995,9 @@ void DocumentWidget::startHighlighting(Verbosity verbosity) {

/* Parse the buffer with pass 1 patterns. If there are none, initialize
the style buffer to all UNFINISHED_STYLE to trigger parsing later */
std::string style_buffer(static_cast<size_t>(bufLength), UNFINISHED_STYLE);
std::basic_string<uint8_t> style_buffer(static_cast<size_t>(bufLength), UNFINISHED_STYLE);
if (highlightData->pass1Patterns) {
char *stylePtr = &style_buffer[0];
uint8_t *stylePtr = &style_buffer[0];

int prev_char = -1;
Highlight::ParseContext ctx;
Expand Down Expand Up @@ -6262,7 +6262,7 @@ std::unique_ptr<WindowHighlightData> DocumentWidget::createHighlightData(Pattern
}

// Create the style buffer
auto styleBuf = std::make_unique<TextBuffer>();
auto styleBuf = std::make_unique<UTextBuffer>();

const int contextLines = patternSet->lineContext;
const int contextChars = patternSet->charContext;
Expand Down
4 changes: 2 additions & 2 deletions src/DocumentWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ class DocumentWidget : public QWidget {
void gotoAP(TextArea *area, int64_t lineNum, int64_t column);
void gotoMark(TextArea *area, QChar label, bool extendSel);
void gotoMatchingCharacter(TextArea *area, bool select);
void handleUnparsedRegion(const std::shared_ptr<TextBuffer> &styleBuf, TextCursor pos) const;
void handleUnparsedRegion(TextBuffer *styleBuf, TextCursor pos) const;
void handleUnparsedRegion(const std::shared_ptr<UTextBuffer> &styleBuf, TextCursor pos) const;
void handleUnparsedRegion(UTextBuffer *styleBuf, TextCursor pos) const;
void macroBannerTimeoutProc();
void makeSelectionVisible(TextArea *area);
void moveDocument(MainWindow *fromWindow);
Expand Down

0 comments on commit 992a70e

Please sign in to comment.