如何在Delphi TStringList中更快地搜索名称/值对? [英] How can I search faster for name/value pairs in a Delphi TStringList?

查看:2007
本文介绍了如何在Delphi TStringList中更快地搜索名称/值对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过将运行时的所有字符串放在TStringList中来实现应用程序中的语言翻译:

  procedure PopulateStringList; 
begin
EnglishStringList.Append('CAN_T_FIND_FILE =无法找到文件');
EnglishStringList.Append('DUMMY = Just a dummy record');
//以相同的方式附加2000个记录的总和
EnglishStringList.Sorted:= True; //更新评论:这是USELESS!
结束

然后我使用以下方式获得翻译:

 函数GetTranslation(ResStr:String):String; 
var
iIndex:Integer;
开始
iIndex:= -1;
iIndex:= EnglishStringList.IndexOfName(ResStr);
如果iIndex> = 0,那么
结果:= EnglishStringList.ValueFromIndex [iIndex] else
结果:= ResStr +'(翻译N / A)';
结束

无论如何,通过这种方法,需要大约30微秒来定位记录,是否有更好的方法来实现相同的结果?



更新:为了将来参考,我在这里写道,使用TDictionary的新实现如建议(与Delphi 2009及更新版本一起使用)

  procedure PopulateStringList; 
begin
EnglishDictionary:= TDictionary< String,String> .Create;
EnglishDictionary.Add('CAN_T_FIND_FILE','无法找到文件');
EnglishDictionary.Add('DUMMY','Just a dummy record');
//总共2000个记录以相同的方式附加
end;


函数GetTranslation(ResStr:String):String;
var
ValueFound:Boolean;
begin
ValueFound:= EnglishDictionary.TryGetValue(ResStr,Result);
如果不是ValueFound则结果:= Result +'(Trans N / A)';
结束

新的GetTranslation函数的执行速度提高了1000倍(在我的2000个样本记录上),然后是第一个版本。 / p>

解决方案

在Delphi 2009或更高版本中,我将使用TDictionary< string,string>在Genericics.Collections中。
另请注意,有免费的工具,如 http://dxgettext.po.dk/ for翻译应用程序。


I implemented language translation in an application by putting all strings at runtime in a TStringList with:

procedure PopulateStringList;
begin  
  EnglishStringList.Append('CAN_T_FIND_FILE=It is not possible to find the file');
  EnglishStringList.Append('DUMMY=Just a dummy record');
  // total of 2000 record appended in the same way
  EnglishStringList.Sorted := True; // Updated comment: this is USELESS!
end;

Then I get the translation using:

function GetTranslation(ResStr:String):String;
var
  iIndex : Integer;
begin
  iIndex := -1;
  iIndex :=  EnglishStringList.IndexOfName(ResStr);
  if iIndex >= 0 then
  Result :=  EnglishStringList.ValueFromIndex[iIndex] else
  Result := ResStr + ' (Translation N/A)';
end;

Anyway with this approach it takes about 30 microseconds to locate a record, is there a better way to achieve the same result?

UPDATE: For future reference I write here the new implementation that uses TDictionary as suggested (works with Delphi 2009 and newer):

procedure PopulateStringList;
begin  
  EnglishDictionary := TDictionary<String, String>.Create;
  EnglishDictionary.Add('CAN_T_FIND_FILE','It is not possible to find the file');
  EnglishDictionary.Add('DUMMY','Just a dummy record');
  // total of 2000 record appended in the same way
end;


function GetTranslation(ResStr:String):String;
var
  ValueFound: Boolean;
begin
  ValueFound:=  EnglishDictionary.TryGetValue(ResStr, Result);
  if not ValueFound then Result := Result + '(Trans N/A)';
end;

The new GetTranslation function performs 1000 times faster (on my 2000 sample records) then the first version.

解决方案

In Delphi 2009 or later I would use TDictionary< string,string > in Generics.Collections. Also note that there are free tools such as http://dxgettext.po.dk/ for translating applications.

这篇关于如何在Delphi TStringList中更快地搜索名称/值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆