-
Notifications
You must be signed in to change notification settings - Fork 22.8k
Add an example to search for multi-byte pattern in an array #39479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
def00111
wants to merge
24
commits into
mdn:main
Choose a base branch
from
def00111:patch-17
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−0
Open
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
29f071f
Add an example to search for multi-byte pattern in an array
def00111 163c953
Update index.md
def00111 3f50141
Update index.md
def00111 234d195
Update example
def00111 171cfe2
Update example
def00111 7610ab6
Update example
def00111 1a987e4
Update example
def00111 d376839
Update example
def00111 d1941c2
Update example
def00111 58c9c75
Update example
def00111 221205a
Update example
def00111 0f18979
Update example
def00111 a3861f4
Update example
def00111 ec57ec4
Update example
def00111 733a893
Update example
def00111 c090a53
Update example
def00111 cc8beca
Update example
def00111 55d6078
Apply suggestions from code review
def00111 a57ebc9
Update example
def00111 2f78c3a
Update example
def00111 66e0312
Apply suggestions from code review
def00111 9756534
Update example
def00111 1fc53ce
Update example
def00111 eef7882
Update example
def00111 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -315,6 +315,147 @@ browser.webRequest.onBeforeRequest.addListener( | |
); | ||
``` | ||
|
||
This example demonstrates, how to search for multi-byte pattern in an array: | ||
|
||
```js | ||
// JavaScript program to search the pattern in given array | ||
// using KMP Algorithm | ||
|
||
function constructLps(pat, lps) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
// len stores the length of longest prefix which | ||
// is also a suffix for the previous index | ||
let len = 0; | ||
|
||
// lps[0] is always 0 | ||
lps[0] = 0; | ||
|
||
let i = 1; | ||
while (i < pat.length) { | ||
// If characters match, increment the size of lps | ||
if (pat[i] === pat[len]) { | ||
len++; | ||
lps[i] = len; | ||
i++; | ||
} | ||
// If there is a mismatch | ||
else { | ||
if (len !== 0) { | ||
// Update len to the previous lps value | ||
// to avoid redundant comparisons | ||
len = lps[len - 1]; | ||
} else { | ||
// If no matching prefix found, set lps[i] to 0 | ||
lps[i] = 0; | ||
i++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function search(pat, arr) { | ||
const n = arr.length; | ||
const m = pat.length; | ||
|
||
const lps = new Array(m); | ||
const res = []; | ||
|
||
constructLps(pat, lps); | ||
|
||
// Pointers i and j, for traversing | ||
// the array and pattern | ||
let i = 0; | ||
let j = 0; | ||
|
||
while (i < n) { | ||
// If characters match, move both pointers forward | ||
if (arr[i] === pat[j]) { | ||
i++; | ||
j++; | ||
|
||
// If the entire pattern is matched | ||
// store the start index in result | ||
if (j === m) { | ||
res.push(i - j); | ||
// Use LPS of previous index to | ||
// skip unnecessary comparisons | ||
j = lps[j - 1]; | ||
} | ||
} | ||
// If there is a mismatch | ||
else { | ||
// Use lps value of previous index | ||
// to avoid redundant comparisons | ||
if (j !== 0) { | ||
j = lps[j - 1]; | ||
} else { | ||
i++; | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
const encoder = new TextEncoder(); | ||
|
||
const elements = encoder.encode("WebExtension "); | ||
const bytes = encoder.encode("Example"); | ||
|
||
function listener(details) { | ||
const filter = browser.webRequest.filterResponseData(details.requestId); | ||
|
||
const oldData = []; | ||
filter.ondata = (event) => { | ||
let data = event.data; | ||
data = new Uint8Array(data); | ||
data = Array.from(data); | ||
|
||
if (oldData.length) { | ||
data = oldData.concat(data); | ||
oldData.length = 0; | ||
} | ||
|
||
let len = 0; | ||
const res = search(bytes, data); | ||
for (const i of res) { | ||
// Insert "WebExtension " at the given index | ||
data.splice(i + len, 0, ...elements); | ||
len += elements.length; | ||
} | ||
|
||
// Check if the end of the data looks like "<h1>Exampl" | ||
const n = data.length; | ||
mainLoop: for ( | ||
let i = n - 1, l = n - bytes.length; | ||
i > l; | ||
i-- | ||
) { | ||
def00111 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (bytes[0] === data[i]) { | ||
const initial = i; | ||
for (let j = 1, l = n - i; j < l; j++) { | ||
if (data[++i] !== bytes[j]) { | ||
break mainLoop; | ||
} | ||
} | ||
oldData.push(...data.slice(initial)); | ||
data = data.slice(0, initial); | ||
break; | ||
} | ||
} | ||
filter.write(new Uint8Array(data)); | ||
}; | ||
|
||
filter.onstop = () => { | ||
filter.close(); | ||
}; | ||
} | ||
|
||
browser.webRequest.onBeforeRequest.addListener( | ||
listener, | ||
{ urls: ["https://example.com/"], types: ["main_frame"] }, | ||
["blocking"], | ||
); | ||
``` | ||
|
||
{{WebExtExamples}} | ||
|
||
## Browser compatibility | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example is very large now. Should it be on https://github.com/mdn/webextensions-examples?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@def00111 yes, I think this is now getting to the point where we need to move it out of the documentation into a web extensions example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened mdn/webextensions-examples#586