Skip to content

Commit

Permalink
use stringbuilder to join strings to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
xulihang committed Dec 27, 2018
1 parent 143567b commit b744d1e
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 82 deletions.
2 changes: 1 addition & 1 deletion BasicCAT/BasicCAT.b4j.meta
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions BasicCAT/Project.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion BasicCAT/SRX.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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,"")
Expand Down
40 changes: 30 additions & 10 deletions BasicCAT/Utils.bas
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,24 @@ 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
Dim noteText As StringBuilder
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion BasicCAT/XMLUtils.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
59 changes: 11 additions & 48 deletions BasicCAT/idmlFilter.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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("</*c[1-9]>",translation)
Do While matcher.Find
source=su.RemoveFirst(source,matcher.Match)
Loop
Dim matcher2 As Matcher
matcher2=Regex.Matcher("</*c[1-9]>",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("</*c[1-9]>",translation)
Do While matcher.Find
source=su.RemoveFirst(source,matcher.Match)
Loop
Dim matcher2 As Matcher
matcher2=Regex.Matcher("</*c[1-9]>",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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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=$"<span id="current" name="current" >${translation}</span>"$
End If
text=text&translation
'text=text&translation
text.Append(translation)
Next
Return text
Return text.ToString
End Sub
16 changes: 10 additions & 6 deletions BasicCAT/txtFilter.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -276,7 +279,8 @@ Sub previewText As String
If i=Main.currentProject.lastEntry Then
translation=$"<span id="current" name="current" >${translation}</span>"$
End If
text=text&translation
'text=text&translation
text.Append(translation)
Next
Return text
Return text.ToString
End Sub
9 changes: 5 additions & 4 deletions BasicCAT/xliffFilter.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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=$"<span id="current" name="current" >${translation}</span>"$
End If
text=text&translation
text.Append(translation)
Next
Return text
Return text.ToString
End Sub
20 changes: 11 additions & 9 deletions plugins/poFilter/poFilterPlugin.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -195,23 +196,23 @@ 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
If msgstr.Contains("\n") Then
'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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit b744d1e

Please sign in to comment.