diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-05-05 16:19:59 +0200 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2018-05-12 17:32:53 +0200 |
commit | 43ec938634a39a38cb687bd866c69940e31f1edd (patch) | |
tree | 9883a9cb833f04bc40a1543afa913275f24df647 | |
parent | f69a823f8eb6dff58faf3785eed19af65cebe811 (diff) | |
download | node-new-43ec938634a39a38cb687bd866c69940e31f1edd.tar.gz |
src: remove static variables from string_search
These variables can as well be stack-allocated. This avoids
relying on global state that is not protected by mutexes.
Thanks to Stephen Belanger for reviewing this change in its original PR.
Refs: https://github.com/ayojs/ayo/pull/82
PR-URL: https://github.com/nodejs/node/pull/20541
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
-rw-r--r-- | node.gyp | 1 | ||||
-rw-r--r-- | src/string_search.cc | 11 | ||||
-rw-r--r-- | src/string_search.h | 38 |
3 files changed, 9 insertions, 41 deletions
@@ -344,7 +344,6 @@ 'src/spawn_sync.cc', 'src/string_bytes.cc', 'src/string_decoder.cc', - 'src/string_search.cc', 'src/stream_base.cc', 'src/stream_pipe.cc', 'src/stream_wrap.cc', diff --git a/src/string_search.cc b/src/string_search.cc deleted file mode 100644 index 326fba7c4a..0000000000 --- a/src/string_search.cc +++ /dev/null @@ -1,11 +0,0 @@ -#include "string_search.h" - -namespace node { -namespace stringsearch { - -int StringSearchBase::kBadCharShiftTable[kUC16AlphabetSize]; -int StringSearchBase::kGoodSuffixShiftTable[kBMMaxShift + 1]; -int StringSearchBase::kSuffixTable[kBMMaxShift + 1]; - -} // namespace stringsearch -} // namespace node diff --git a/src/string_search.h b/src/string_search.h index 6a45eb3666..9c090da247 100644 --- a/src/string_search.h +++ b/src/string_search.h @@ -80,12 +80,12 @@ class StringSearchBase { static const int kBMMinPatternLength = 8; // Store for the BoyerMoore(Horspool) bad char shift table. - static int kBadCharShiftTable[kUC16AlphabetSize]; + int bad_char_shift_table_[kUC16AlphabetSize]; // Store for the BoyerMoore good suffix shift table. - static int kGoodSuffixShiftTable[kBMMaxShift + 1]; + int good_suffix_shift_table_[kBMMaxShift + 1]; // Table used temporarily while building the BoyerMoore good suffix // shift table. - static int kSuffixTable[kBMMaxShift + 1]; + int suffix_table_[kBMMaxShift + 1]; }; template <typename Char> @@ -152,26 +152,6 @@ class StringSearch : private StringSearchBase { return bad_char_occurrence[equiv_class]; } - // Store for the BoyerMoore(Horspool) bad char shift table. - // Return a table covering the last kBMMaxShift+1 positions of - // pattern. - int* bad_char_table() { return kBadCharShiftTable; } - - // Store for the BoyerMoore good suffix shift table. - int* good_suffix_shift_table() { - // Return biased pointer that maps the range [start_..pattern_.length() - // to the kGoodSuffixShiftTable array. - return kGoodSuffixShiftTable - start_; - } - - // Table used temporarily while building the BoyerMoore good suffix - // shift table. - int* suffix_table() { - // Return biased pointer that maps the range [start_..pattern_.length() - // to the kSuffixTable array. - return kSuffixTable - start_; - } - // The pattern to search for. Vector pattern_; // Pointer to implementation of the search. @@ -345,8 +325,8 @@ size_t StringSearch<Char>::BoyerMooreSearch( // Only preprocess at most kBMMaxShift last characters of pattern. size_t start = start_; - int* bad_char_occurrence = bad_char_table(); - int* good_suffix_shift = good_suffix_shift_table(); + int* bad_char_occurrence = bad_char_shift_table_; + int* good_suffix_shift = good_suffix_shift_table_ - start_; Char last_char = pattern_[pattern_length - 1]; size_t index = start_index; @@ -397,8 +377,8 @@ void StringSearch<Char>::PopulateBoyerMooreTable() { // Biased tables so that we can use pattern indices as table indices, // even if we only cover the part of the pattern from offset start. - int* shift_table = good_suffix_shift_table(); - int* suffix_table = this->suffix_table(); + int* shift_table = good_suffix_shift_table_ - start_; + int* suffix_table = suffix_table_ - start_; // Initialize table. for (size_t i = start; i < pattern_length; i++) { @@ -462,7 +442,7 @@ size_t StringSearch<Char>::BoyerMooreHorspoolSearch( size_t start_index) { const size_t subject_length = subject.length(); const size_t pattern_length = pattern_.length(); - int* char_occurrences = bad_char_table(); + int* char_occurrences = bad_char_shift_table_; int64_t badness = -pattern_length; // How bad we are doing without a good-suffix table. @@ -511,7 +491,7 @@ template <typename Char> void StringSearch<Char>::PopulateBoyerMooreHorspoolTable() { const size_t pattern_length = pattern_.length(); - int* bad_char_occurrence = bad_char_table(); + int* bad_char_occurrence = bad_char_shift_table_; // Only preprocess at most kBMMaxShift last characters of pattern. const size_t start = start_; |