RosettaCodeData/Task/Boyer-Moore-string-search/Dart/boyer-moore-string-search.dart
2025-08-11 18:05:26 -07:00

80 lines
2.9 KiB
Dart
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:convert'; // for jsonEncode (used to mimic std::quoted)
import 'dart:io';
/// Prints a list of integers in the form `[1, 2, 3]`.
void display(List<int> numbers) {
final joined = numbers.join(', ');
print('[$joined]');
}
/// Returns the first index of `needle` inside `haystack`,
/// or -1 if `needle` cannot be found.
int stringSearchSingle(String haystack, String needle) {
final idx = haystack.indexOf(needle);
return idx >= 0 ? idx : -1;
}
/// Returns **all** nonoverlapping start indexes of `needle` inside `haystack`.
List<int> stringSearch(String haystack, String needle) {
final List<int> result = [];
int start = 0; // where we continue searching from
int index = 0; // result of the latest search
// Keep searching while we still find matches and we havent run off the end.
while (start < haystack.length && (index = stringSearchSingle(
haystack.substring(start), needle)) >= 0) {
final absoluteIdx = start + index;
result.add(absoluteIdx);
// Move past the match we just found so the next search starts after it.
start = absoluteIdx + needle.length;
}
return result;
}
void main() {
// ---------------------------------------------------------------------------
// 1⃣ The data (identical to the C++ version)
// ---------------------------------------------------------------------------
const List<String> texts = [
"GCTAGCTCTACGAGTCTA",
"GGCTATAATGCGTA",
"there would have been a time for such a word",
"needle need noodle needle",
"DKnuthusesandprogramsanimaginarycomputertheMIXanditsassociatedmachinecodeandassemblylanguages",
"Nearby farms grew an acre of alfalfa on the dairy's behalf, with bales of that alfalfa exchanged for milk."
];
const List<String> patterns = [
"TCTA",
"TAATAAA",
"word",
"needle",
"and",
"alfalfa"
];
// ---------------------------------------------------------------------------
// 2⃣ Print the original texts (mirrors the first loop in C++).
// ---------------------------------------------------------------------------
for (var i = 0; i < texts.length; ++i) {
print('text${i + 1} = ${texts[i]}');
}
print(''); // blank line like the C++ program
// ---------------------------------------------------------------------------
// 3⃣ Search each pattern in its corresponding text and print the results.
// ---------------------------------------------------------------------------
for (var i = 0; i < texts.length; ++i) {
final List<int> indexes = stringSearch(texts[i], patterns[i]);
// `jsonEncode` adds double quotes around a string and escapes characters,
// which behaves like `std::quoted` in C++.
final quotedPattern = jsonEncode(patterns[i]);
stdout.write(
'Found $quotedPattern in \'text${i + 1}\' at indexes ');
display(indexes);
}
}