MCPcopy Create free account
hub / github.com/LadybugDB/ladybug / highlightSearchMatch

Function highlightSearchMatch

tools/shell/linenoise.cpp:2451–2550  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2449}
2450
2451static void highlightSearchMatch(char* buffer, char* resultBuf, const char* search_buffer) {
2452 const char* matchUnderlinePrefix = "\033[4m";
2453 const char* matchUnderlineResetPostfix = "\033[24m";
2454
2455 std::string searchPhrase(search_buffer);
2456 std::string buf(buffer);
2457 std::string underlinedBuf = "";
2458
2459 std::string matchedWord;
2460 auto searchPhrasePos = 0u;
2461 bool matching = false;
2462 bool matched = false;
2463 bool ansiCode = false;
2464
2465 if (mlmode) {
2466 // Replace newlines with spaces
2467 std::regex newline_regex("[\n\r]");
2468 buf = std::regex_replace(buf, newline_regex, " ");
2469 }
2470
2471 for (auto i = 0u; i < buf.length(); i++) {
2472 if (matched) {
2473 underlinedBuf += buf[i];
2474 continue;
2475 }
2476 if (matching) {
2477 if (searchPhrasePos >= searchPhrase.length()) {
2478 underlinedBuf += std::string(matchUnderlinePrefix);
2479 underlinedBuf += matchedWord;
2480 underlinedBuf += std::string(matchUnderlineResetPostfix);
2481 underlinedBuf += buf[i];
2482 matched = true;
2483 continue;
2484 }
2485 if (ansiCode) {
2486 if (tolower(buf[i]) == 'm') {
2487 ansiCode = false;
2488 }
2489 } else if (tolower(buf[i]) == tolower(searchPhrase[searchPhrasePos])) {
2490 searchPhrasePos++;
2491 } else if (buf[i] == '\033') {
2492 ansiCode = true;
2493 } else {
2494 auto matchIndex = 0u;
2495 // find the oldest place where the match restarts
2496 for (auto j = i; j > i - matchedWord.length(); j--) {
2497 if (tolower(buf[j]) == tolower(searchPhrase[0])) {
2498 matchIndex = j;
2499 }
2500 }
2501 if (matchIndex > 0) {
2502 // restart matching at matchIndex
2503 for (auto j = i - matchedWord.length(); j < matchIndex; j++) {
2504 underlinedBuf += buf[j];
2505 }
2506 i = matchIndex;
2507 matchedWord = buf[i];
2508 searchPhrasePos = 1;

Callers 1

refreshSearchMultiLineFunction · 0.85

Calls 2

lengthMethod · 0.80
emptyMethod · 0.45

Tested by

no test coverage detected