我有一個名爲Strings.h的文件,用於本地化我擁有的應用程序。我想搜索我的所有類文件,並查明我是否在哪裏使用每個字符串,並輸出每個字符串的類和行號。從文件中提取單詞,然後列出文件以及包含這些單詞的行號
我的想法是使用Python,但也許這是工作的錯誤工具。另外,我有一個基本的算法,但是我擔心它運行需要很長時間。你能寫這個腳本來做我想做的事嗎,或者甚至只是建議一個更好的算法?
Strings.h看起來是這樣的:
#import "NonLocalizedStrings.h"
#pragma mark Coordinate Behavior Strings
#define LATITUDE_WORD NSLocalizedString(@"Latitude", @"used in coordinate behaviors")
#define LONGITUDE_WORD NSLocalizedString(@"Longitude", @"used in coordinate behaviors")
#define DEGREES_WORD NSLocalizedString(@"Degrees", @"used in coordinate behaviors")
#define MINUTES_WORD NSLocalizedString(@"Minutes", @"Used in coordiante behaviors")
#define SECONDS_WORD NSLocalizedString(@"Seconds", @"Used in DMSBehavior.m")
...
腳本應該採取每個以#定義啓動線,然後進行中的#define(例如)LATITUDE_WORD
後出現的單詞的列表僞代碼可能是:
file = strings.h
for line in file:
extract word after #define
search_words.push(word)
print search_words
[LATITUDE_WORD, LONGITUDE_WORD, DEGREES_WORD, MINUTES_WORD, SECONDS WORD]
後,我有話清單,我的僞代碼是一樣的東西:
found_words = {}
for word in words:
found_words[word] = []
for file in files:
for line in file:
for word in search_words:
if line contains word:
found_words[word].push((filename, linenumber))
print found_words
因此,發現的話會看起來像:
{
LATITUDE_WORD: [
(foo.m, 42),
(bar.m, 132)
],
LONGITUDE_WORD: [
(baz.m, 22),
(bim.m, 112)
],
}
您可以更新它來遞歸搜索一個目錄,並輸出任何從未找到的單詞嗎?我認爲這可能是最好的解決方案,我很抱歉更新要求! –
它已經遞歸搜索'project_dir'以查找匹配'* .m'的文件。鑑於其工作方式,輸出永遠不會找到的單詞更爲棘手。 –
好吧,編輯後輸出未找到的單詞。花了我一段時間想通過... –