我正在嘗試實作 grep 的 -A -B -C 標志背后的邏輯。為了了解事情是如何作業的,我克隆了 grep C 代碼并對其進行了研究。但是我不擅長C,所以我發現它非常困難。
假設我需要在字串陣列中搜索一段字串。我可以輕松做到這一點。我還將匹配的索引號保存在另一個陣列中。
現在,我如何實作這些標志的邏輯。我不需要代碼,我只想了解邏輯:)
uj5u.com熱心網友回復:
作為開始,我會保留一個緩沖區來保存最后A讀取的行(或者C,取決于指定的內容)。默認情況下,此緩沖區的大小為 1。
一旦 grep 找到它正在搜索的模式,它就可以輸出緩沖區和連續B(或C)行。
要考慮的事情是:如果 grep 在尾隨B行中發現其搜索模式的另一個出現,如何做出反應。我可能會選擇列印另一B行。
uj5u.com熱心網友回復:
讓我們通過示例來展示:
index of match = 3
length of lines = 5
A = 2
print lines with indices max(0, 3-2) to 3, inclusive
B = 2
print lines with indices 3 to min(5-1, 3 2), inclusive
C = 3
print lines with indices max(0, 3-3) to min(5-1, 3 3), inclusive
換句話說,請確保您不會超出您的行數范圍,而只是根據標志向前、向后或兩者兼而有之。
如果有多個標志,請執行以下操作:
If A and B: set the min and max bounds accorddingly
If A and C: set the min as min of A and C
and max of the result of the previous calculation with 0
to not go out of bounds.
...
如果一個匹配的下限與前一個匹配的上限重疊,則上述演算法可能會產生重復的行。一種解釋方法如下:
For each match, if it is not the first in your matches array,
set its lower bound as the max of its lower bound
and the upper bound of the previous match.
If the result is greater than the match itself, set it to the match.
This will produce one duplicate line (the match itself)
but you probably want to keep that for presentation purposes.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/417561.html
標籤:
上一篇:迭代演算法變成遞回演算法
下一篇:階乘的模除以階乘
