From b744d1ec01962092cf20fdb231457e64f4bd6505 Mon Sep 17 00:00:00 2001 From: xulihang Date: Thu, 27 Dec 2018 12:14:12 +0800 Subject: [PATCH] use stringbuilder to join strings to improve performance --- BasicCAT/BasicCAT.b4j.meta | 2 +- BasicCAT/Project.bas | 4 +- BasicCAT/SRX.bas | 2 +- BasicCAT/Utils.bas | 40 ++++++++++++++----- BasicCAT/XMLUtils.bas | 4 +- BasicCAT/idmlFilter.bas | 59 ++++++----------------------- BasicCAT/txtFilter.bas | 16 +++++--- BasicCAT/xliffFilter.bas | 9 +++-- plugins/poFilter/poFilterPlugin.bas | 20 +++++----- 9 files changed, 74 insertions(+), 82 deletions(-) diff --git a/BasicCAT/BasicCAT.b4j.meta b/BasicCAT/BasicCAT.b4j.meta index 298615a..742d6fb 100644 --- a/BasicCAT/BasicCAT.b4j.meta +++ b/BasicCAT/BasicCAT.b4j.meta @@ -182,4 +182,4 @@ ModuleClosedNodes7= ModuleClosedNodes8= ModuleClosedNodes9= SelectedBuild=0 -VisibleModules=1,2,3,4,5,6,7,8,9,10 +VisibleModules=1,2,3,4,5,6,7,8,9,10,15,16,41,11,21,50 diff --git a/BasicCAT/Project.bas b/BasicCAT/Project.bas index 06b31da..fdae176 100644 --- a/BasicCAT/Project.bas +++ b/BasicCAT/Project.bas @@ -726,7 +726,7 @@ Sub exportMarkdownWithNotesMi_Action Dim exportPath As String exportPath=fc.ShowSave(Main.MainForm) If exportPath<>"" Then - Utils.exportToMarkdownWithNotes(segments,exportPath,currentFilename,projectFile.Get("source"),projectFile.Get("target"),settings) + Utils.exportToMarkdownWithNotes(segments,exportPath,currentFilename,projectFile.Get("source"),projectFile.Get("target"),settings,path) fx.Msgbox(Main.MainForm,"Done.","") End If End Sub @@ -746,7 +746,7 @@ Sub exportBiParagraphMi_Action Dim exportPath As String exportPath=fc.ShowSave(Main.MainForm) If exportPath<>"" Then - Utils.exportToBiParagraph(segments,exportPath,currentFilename,projectFile.Get("source"),projectFile.Get("target"),settings) + Utils.exportToBiParagraph(segments,exportPath,currentFilename,projectFile.Get("source"),projectFile.Get("target"),settings,path) fx.Msgbox(Main.MainForm,"Done.","") End If End Sub diff --git a/BasicCAT/SRX.bas b/BasicCAT/SRX.bas index 3b3d2e0..47b58c7 100644 --- a/BasicCAT/SRX.bas +++ b/BasicCAT/SRX.bas @@ -14,7 +14,7 @@ Sub readRules(filepath As String,lang As String) As Map Dim srxString As String - If filepath="" Then + If filepath="" or File.Exists(filepath,"")=False Then srxString=File.ReadString(File.DirAssets,"segmentationRules.srx") Else srxString=File.ReadString(filepath,"") diff --git a/BasicCAT/Utils.bas b/BasicCAT/Utils.bas index ab97b3c..0b81dbb 100644 --- a/BasicCAT/Utils.bas +++ b/BasicCAT/Utils.bas @@ -121,7 +121,7 @@ Sub shouldAddSpace(sourceLang As String,targetLang As String,index As Int,segmen Return False End Sub -Sub exportToMarkdownWithNotes(segments As List,path As String,filename As String,sourceLang As String,targetLang As String,settings As Map) +Sub exportToMarkdownWithNotes(segments As List,path As String,filename As String,sourceLang As String,targetLang As String,settings As Map,projectPath As String) Dim text As StringBuilder text.Initialize Dim noteIndex As Int=0 @@ -129,13 +129,16 @@ Sub exportToMarkdownWithNotes(segments As List,path As String,filename As String noteText.Initialize noteText.Append(CRLF).Append(CRLF) Dim previousID As String="-1" + Dim previousInnerFilename As String="" Dim index As Int=-1 For Each segment As List In segments index=index+1 Dim source,target,fullsource As String Dim translation As String + Dim innerFilename As String source=segment.Get(0) target=segment.Get(1) + innerFilename=segment.Get(3) If target="" Then target=source Else @@ -162,12 +165,18 @@ Sub exportToMarkdownWithNotes(segments As List,path As String,filename As String previousID=id End If End If + If innerFilename<>previousInnerFilename Then + fullsource=CRLF&fullsource + previousInnerFilename=innerFilename + End If source=Regex.Replace2("<.*?>",32,source,"") target=Regex.Replace2("<.*?>",32,target,"") fullsource=Regex.Replace2("<.*?>",32,fullsource,"") translation=fullsource.Replace(source,target) If LanguageHasSpace(targetLang)=False Then - translation=segmentation.removeSpacesAtBothSides(path,targetLang,translation,settings.GetDefault("remove_space",True)) + If source<>target Then + translation=segmentation.removeSpacesAtBothSides(projectPath,targetLang,translation,settings.GetDefault("remove_space",True)) + End If End If text.Append(translation) Next @@ -177,19 +186,24 @@ Sub exportToMarkdownWithNotes(segments As List,path As String,filename As String File.WriteString(path,"",result) End Sub -Sub exportToBiParagraph(segments As List,path As String,filename As String,sourceLang As String,targetLang As String,settings As Map) +Sub exportToBiParagraph(segments As List,path As String,filename As String,sourceLang As String,targetLang As String,settings As Map,projectPath As String) Dim text As StringBuilder text.Initialize - Dim sourceText As String - Dim targetText As String + Dim sourceText As StringBuilder + sourceText.Initialize + Dim targetText As StringBuilder + targetText.Initialize Dim previousID As String="-1" + Dim previousInnerFilename As String="" Dim index As Int=-1 For Each segment As List In segments index=index+1 Dim source,target,fullsource As String Dim translation As String + Dim innerFilename As String source=segment.Get(0) target=segment.Get(1) + innerFilename=segment.Get(3) If shouldAddSpace(sourceLang,targetLang,index,segments) Then target=target&" " End If @@ -204,21 +218,27 @@ Sub exportToBiParagraph(segments As List,path As String,filename As String,sourc previousID=id End If End If + If innerFilename<>previousInnerFilename Then + fullsource=CRLF&fullsource + previousInnerFilename=innerFilename + End If source=Regex.Replace2("<.*?>",32,source,"") target=Regex.Replace2("<.*?>",32,target,"") fullsource=Regex.Replace2("<.*?>",32,fullsource,"") translation=fullsource.Replace(source,target) If LanguageHasSpace(targetLang)=False Then - translation=segmentation.removeSpacesAtBothSides(path,targetLang,translation,settings.GetDefault("remove_space",True)) + If source<>target Then + translation=segmentation.removeSpacesAtBothSides(projectPath,targetLang,translation,settings.GetDefault("remove_space",True)) + End If End If - sourceText=sourceText&fullsource - targetText=targetText&translation + sourceText.Append(fullsource) + targetText.Append(translation) Next Dim sourceList,targetList As List sourceList.Initialize targetList.Initialize - sourceList.AddAll(Regex.Split(CRLF,sourceText)) - targetList.AddAll(Regex.Split(CRLF,targetText)) + sourceList.AddAll(Regex.Split(CRLF,sourceText.ToString)) + targetList.AddAll(Regex.Split(CRLF,targetText.ToString)) For i=0 To Min(sourceList.Size,targetList.Size)-1 text.Append(sourceList.Get(i)).Append(CRLF) text.Append(targetList.Get(i)).Append(CRLF).Append(CRLF) diff --git a/BasicCAT/XMLUtils.bas b/BasicCAT/XMLUtils.bas index 8f73ce0..9a7a40a 100644 --- a/BasicCAT/XMLUtils.bas +++ b/BasicCAT/XMLUtils.bas @@ -63,13 +63,15 @@ Sub escapedText(xmlstring As String,tagName As String,filetype As String) As Str End If Loop Log(replaceList) + + For Each replacement As Map In replaceList Dim startIndex,endIndex As Int Dim group As String startIndex=replacement.Get("start") endIndex=replacement.Get("end") group=replacement.Get("group") - Dim sb As StringBuilder + Dim sb As StringBuilder sb.Initialize sb.Append(new.SubString2(0,startIndex)) sb.Append(group) diff --git a/BasicCAT/idmlFilter.bas b/BasicCAT/idmlFilter.bas index 34de694..34a97b5 100644 --- a/BasicCAT/idmlFilter.bas +++ b/BasicCAT/idmlFilter.bas @@ -829,38 +829,6 @@ Sub replaceStyleAndFontFileForZh(unzipedDirPath As String) File.Copy(File.DirAssets,"Fonts.xml",File.Combine(unzipedDirPath,"Resources"),"Fonts.xml") End Sub -Sub isTagMissing(translation As String,source As String) As Boolean - Dim su As ApacheSU - Dim matcher As Matcher - matcher=Regex.Matcher("",translation) - Do While matcher.Find - source=su.RemoveFirst(source,matcher.Match) - Loop - Dim matcher2 As Matcher - matcher2=Regex.Matcher("",source) - If matcher2.Find Then - Return True - Else - Return False - End If -End Sub - -Sub MissingTagAddedText(translation As String,source As String) As String - - Dim su As ApacheSU - Dim matcher As Matcher - matcher=Regex.Matcher("",translation) - Do While matcher.Find - source=su.RemoveFirst(source,matcher.Match) - Loop - Dim matcher2 As Matcher - matcher2=Regex.Matcher("",source) - Do While matcher2.Find - translation=translation&matcher2.Match - Loop - Return translation -End Sub - Sub textToListInOrder(pureText As String) As List Dim result As List @@ -1162,15 +1130,6 @@ Sub NextIsLower(XY1 As coordinate,XY2 As coordinate) As Boolean End If End Sub -Sub stripContent(content As String) As String - content=Regex.Replace(" +", content," ") - content=content.Replace("
","") - content=content.Replace(" "," ") - content=Regex.Replace("\n\n+",content,CRLF&CRLF) - Return content -End Sub - - Sub TextFramesListOfEachSpread(spreadMap As Map) As List Dim root As Map root=spreadMap.Get("idPkg:Spread") @@ -1360,15 +1319,14 @@ End Sub Sub previewText As String - Dim text As String + Dim text As StringBuilder + text.Initialize If Main.editorLV.Items.Size<>Main.currentProject.segments.Size Then Return "" End If + Dim previousStory as String For i=Max(0,Main.currentProject.lastEntry-3) To Min(Main.currentProject.lastEntry+7,Main.currentProject.segments.Size-1) - - Try - Dim p As Pane p=Main.editorLV.Items.Get(i) Catch @@ -1417,11 +1375,16 @@ Sub previewText As String translation=segmentation.removeSpacesAtBothSides(Main.currentProject.path,Main.currentProject.projectFile.Get("target"),translation,Utils.getMap("settings",Main.currentProject.projectFile).GetDefault("remove_space",True)) End If End If - + Dim story As String=bitext.Get(3) + If previousStory<>story Then + text.Append(CRLF) + previousStory=story + End If If i=Main.currentProject.lastEntry Then translation=$"${translation}"$ End If - text=text&translation + 'text=text&translation + text.Append(translation) Next - Return text + Return text.ToString End Sub \ No newline at end of file diff --git a/BasicCAT/txtFilter.bas b/BasicCAT/txtFilter.bas index 91649aa..a6cbdf2 100644 --- a/BasicCAT/txtFilter.bas +++ b/BasicCAT/txtFilter.bas @@ -53,7 +53,8 @@ End Sub Sub generateFile(filename As String,path As String,projectFile As Map) Dim innerfilename As String=filename - Dim result As String + Dim result As StringBuilder + result.Initialize Dim workfile As Map Dim json As JSONParser json.Initialize(File.ReadString(File.Combine(path,"work"),filename&".json")) @@ -95,11 +96,12 @@ Sub generateFile(filename As String,path As String,projectFile As Map) End If End If - result=result&translation + 'result=result&translation + result.Append(translation) Next Next - File.WriteString(File.Combine(path,"target"),filename,result) + File.WriteString(File.Combine(path,"target"),filename,result.ToString) Main.updateOperation(filename&" generated!") End Sub @@ -236,7 +238,8 @@ Sub splitSegment(sourceTextArea As TextArea) End Sub Sub previewText As String - Dim text As String + Dim text As StringBuilder + text.Initialize If Main.editorLV.Items.Size<>Main.currentProject.segments.Size Then Return "" End If @@ -276,7 +279,8 @@ Sub previewText As String If i=Main.currentProject.lastEntry Then translation=$"${translation}"$ End If - text=text&translation + 'text=text&translation + text.Append(translation) Next - Return text + Return text.ToString End Sub \ No newline at end of file diff --git a/BasicCAT/xliffFilter.bas b/BasicCAT/xliffFilter.bas index cc016fc..063d18f 100644 --- a/BasicCAT/xliffFilter.bas +++ b/BasicCAT/xliffFilter.bas @@ -510,7 +510,8 @@ Sub splitSegment(sourceTextArea As TextArea) End Sub Sub previewText As String - Dim text As String + Dim text As StringBuilder + text.Initialize If Main.editorLV.Items.Size<>Main.currentProject.segments.Size Then Return "" End If @@ -553,13 +554,13 @@ Sub previewText As String Dim id As String id=extra.Get("id") If previousID<>id Then - text=text&CRLF + text.Append(CRLF) previousID=id End If If i=Main.currentProject.lastEntry Then translation=$"${translation}"$ End If - text=text&translation + text.Append(translation) Next - Return text + Return text.ToString End Sub \ No newline at end of file diff --git a/plugins/poFilter/poFilterPlugin.bas b/plugins/poFilter/poFilterPlugin.bas index b863429..640423e 100644 --- a/plugins/poFilter/poFilterPlugin.bas +++ b/plugins/poFilter/poFilterPlugin.bas @@ -181,7 +181,8 @@ Sub countMsgStr(path As String,filename As String) As Int End Sub Sub fillPO(msgstrList As List,path As String,filename As String) As String - Dim content As String + Dim content As StringBuilder + content.Initialize Dim textReader As TextReader @@ -195,7 +196,7 @@ Sub fillPO(msgstrList As List,path As String,filename As String) As String msgstr=msgstrList.Get(0) If msgstr="" Then msgstrList.RemoveAt(0) - content=content&line&CRLF + content.Append(line).Append(CRLF) line=textReader.ReadLine Continue End If @@ -203,15 +204,15 @@ Sub fillPO(msgstrList As List,path As String,filename As String) As String 'Log(True) msgstr=handleMultiline(msgstr) End If - content=content&"msgstr "&Chr(34)&msgstr&Chr(34)&CRLF&CRLF + content.Append("msgstr ").Append(Chr(34)).Append(msgstr).Append(Chr(34)).Append(CRLF).Append(CRLF) msgstrList.RemoveAt(0) Else - content=content&line&CRLF + content.Append(line).Append(CRLF) End If line=textReader.ReadLine Loop textReader.Close - Return content + Return content.ToString End Sub Sub handleMultiline(text As String) As String @@ -503,7 +504,8 @@ End Sub Sub previewText(editorLV As ListView,segments As List,lastEntry As Int,sourceLang As String,targetLang As String,path As String,settings As Map) As String Log("Po preview") - Dim text As String + Dim text As StringBuilder + text.Initialize If editorLV.Items.Size<>segments.Size Then Return "" End If @@ -547,10 +549,10 @@ Sub previewText(editorLV As ListView,segments As List,lastEntry As Int,sourceLan id=extra.Get("id") If previousID<>id Then - text=text&CRLF + text.Append(CRLF) previousID=id End If - text=text&translation + text.Append(translation) Next - Return text + Return text.ToString End Sub \ No newline at end of file