Skip to content

Commit b744d1e

Browse files
committed
use stringbuilder to join strings to improve performance
1 parent 143567b commit b744d1e

9 files changed

+74
-82
lines changed

BasicCAT/BasicCAT.b4j.meta

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,4 @@ ModuleClosedNodes7=
182182
ModuleClosedNodes8=
183183
ModuleClosedNodes9=
184184
SelectedBuild=0
185-
VisibleModules=1,2,3,4,5,6,7,8,9,10
185+
VisibleModules=1,2,3,4,5,6,7,8,9,10,15,16,41,11,21,50

BasicCAT/Project.bas

+2-2
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ Sub exportMarkdownWithNotesMi_Action
726726
Dim exportPath As String
727727
exportPath=fc.ShowSave(Main.MainForm)
728728
If exportPath<>"" Then
729-
Utils.exportToMarkdownWithNotes(segments,exportPath,currentFilename,projectFile.Get("source"),projectFile.Get("target"),settings)
729+
Utils.exportToMarkdownWithNotes(segments,exportPath,currentFilename,projectFile.Get("source"),projectFile.Get("target"),settings,path)
730730
fx.Msgbox(Main.MainForm,"Done.","")
731731
End If
732732
End Sub
@@ -746,7 +746,7 @@ Sub exportBiParagraphMi_Action
746746
Dim exportPath As String
747747
exportPath=fc.ShowSave(Main.MainForm)
748748
If exportPath<>"" Then
749-
Utils.exportToBiParagraph(segments,exportPath,currentFilename,projectFile.Get("source"),projectFile.Get("target"),settings)
749+
Utils.exportToBiParagraph(segments,exportPath,currentFilename,projectFile.Get("source"),projectFile.Get("target"),settings,path)
750750
fx.Msgbox(Main.MainForm,"Done.","")
751751
End If
752752
End Sub

BasicCAT/SRX.bas

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Sub readRules(filepath As String,lang As String) As Map
1414
Dim srxString As String
1515

1616

17-
If filepath="" Then
17+
If filepath="" or File.Exists(filepath,"")=False Then
1818
srxString=File.ReadString(File.DirAssets,"segmentationRules.srx")
1919
Else
2020
srxString=File.ReadString(filepath,"")

BasicCAT/Utils.bas

+30-10
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,24 @@ Sub shouldAddSpace(sourceLang As String,targetLang As String,index As Int,segmen
121121
Return False
122122
End Sub
123123

124-
Sub exportToMarkdownWithNotes(segments As List,path As String,filename As String,sourceLang As String,targetLang As String,settings As Map)
124+
Sub exportToMarkdownWithNotes(segments As List,path As String,filename As String,sourceLang As String,targetLang As String,settings As Map,projectPath As String)
125125
Dim text As StringBuilder
126126
text.Initialize
127127
Dim noteIndex As Int=0
128128
Dim noteText As StringBuilder
129129
noteText.Initialize
130130
noteText.Append(CRLF).Append(CRLF)
131131
Dim previousID As String="-1"
132+
Dim previousInnerFilename As String=""
132133
Dim index As Int=-1
133134
For Each segment As List In segments
134135
index=index+1
135136
Dim source,target,fullsource As String
136137
Dim translation As String
138+
Dim innerFilename As String
137139
source=segment.Get(0)
138140
target=segment.Get(1)
141+
innerFilename=segment.Get(3)
139142
If target="" Then
140143
target=source
141144
Else
@@ -162,12 +165,18 @@ Sub exportToMarkdownWithNotes(segments As List,path As String,filename As String
162165
previousID=id
163166
End If
164167
End If
168+
If innerFilename<>previousInnerFilename Then
169+
fullsource=CRLF&fullsource
170+
previousInnerFilename=innerFilename
171+
End If
165172
source=Regex.Replace2("<.*?>",32,source,"")
166173
target=Regex.Replace2("<.*?>",32,target,"")
167174
fullsource=Regex.Replace2("<.*?>",32,fullsource,"")
168175
translation=fullsource.Replace(source,target)
169176
If LanguageHasSpace(targetLang)=False Then
170-
translation=segmentation.removeSpacesAtBothSides(path,targetLang,translation,settings.GetDefault("remove_space",True))
177+
If source<>target Then
178+
translation=segmentation.removeSpacesAtBothSides(projectPath,targetLang,translation,settings.GetDefault("remove_space",True))
179+
End If
171180
End If
172181
text.Append(translation)
173182
Next
@@ -177,19 +186,24 @@ Sub exportToMarkdownWithNotes(segments As List,path As String,filename As String
177186
File.WriteString(path,"",result)
178187
End Sub
179188

180-
Sub exportToBiParagraph(segments As List,path As String,filename As String,sourceLang As String,targetLang As String,settings As Map)
189+
Sub exportToBiParagraph(segments As List,path As String,filename As String,sourceLang As String,targetLang As String,settings As Map,projectPath As String)
181190
Dim text As StringBuilder
182191
text.Initialize
183-
Dim sourceText As String
184-
Dim targetText As String
192+
Dim sourceText As StringBuilder
193+
sourceText.Initialize
194+
Dim targetText As StringBuilder
195+
targetText.Initialize
185196
Dim previousID As String="-1"
197+
Dim previousInnerFilename As String=""
186198
Dim index As Int=-1
187199
For Each segment As List In segments
188200
index=index+1
189201
Dim source,target,fullsource As String
190202
Dim translation As String
203+
Dim innerFilename As String
191204
source=segment.Get(0)
192205
target=segment.Get(1)
206+
innerFilename=segment.Get(3)
193207
If shouldAddSpace(sourceLang,targetLang,index,segments) Then
194208
target=target&" "
195209
End If
@@ -204,21 +218,27 @@ Sub exportToBiParagraph(segments As List,path As String,filename As String,sourc
204218
previousID=id
205219
End If
206220
End If
221+
If innerFilename<>previousInnerFilename Then
222+
fullsource=CRLF&fullsource
223+
previousInnerFilename=innerFilename
224+
End If
207225
source=Regex.Replace2("<.*?>",32,source,"")
208226
target=Regex.Replace2("<.*?>",32,target,"")
209227
fullsource=Regex.Replace2("<.*?>",32,fullsource,"")
210228
translation=fullsource.Replace(source,target)
211229
If LanguageHasSpace(targetLang)=False Then
212-
translation=segmentation.removeSpacesAtBothSides(path,targetLang,translation,settings.GetDefault("remove_space",True))
230+
If source<>target Then
231+
translation=segmentation.removeSpacesAtBothSides(projectPath,targetLang,translation,settings.GetDefault("remove_space",True))
232+
End If
213233
End If
214-
sourceText=sourceText&fullsource
215-
targetText=targetText&translation
234+
sourceText.Append(fullsource)
235+
targetText.Append(translation)
216236
Next
217237
Dim sourceList,targetList As List
218238
sourceList.Initialize
219239
targetList.Initialize
220-
sourceList.AddAll(Regex.Split(CRLF,sourceText))
221-
targetList.AddAll(Regex.Split(CRLF,targetText))
240+
sourceList.AddAll(Regex.Split(CRLF,sourceText.ToString))
241+
targetList.AddAll(Regex.Split(CRLF,targetText.ToString))
222242
For i=0 To Min(sourceList.Size,targetList.Size)-1
223243
text.Append(sourceList.Get(i)).Append(CRLF)
224244
text.Append(targetList.Get(i)).Append(CRLF).Append(CRLF)

BasicCAT/XMLUtils.bas

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ Sub escapedText(xmlstring As String,tagName As String,filetype As String) As Str
6363
End If
6464
Loop
6565
Log(replaceList)
66+
67+
6668
For Each replacement As Map In replaceList
6769
Dim startIndex,endIndex As Int
6870
Dim group As String
6971
startIndex=replacement.Get("start")
7072
endIndex=replacement.Get("end")
7173
group=replacement.Get("group")
72-
Dim sb As StringBuilder
74+
Dim sb As StringBuilder
7375
sb.Initialize
7476
sb.Append(new.SubString2(0,startIndex))
7577
sb.Append(group)

BasicCAT/idmlFilter.bas

+11-48
Original file line numberDiff line numberDiff line change
@@ -829,38 +829,6 @@ Sub replaceStyleAndFontFileForZh(unzipedDirPath As String)
829829
File.Copy(File.DirAssets,"Fonts.xml",File.Combine(unzipedDirPath,"Resources"),"Fonts.xml")
830830
End Sub
831831

832-
Sub isTagMissing(translation As String,source As String) As Boolean
833-
Dim su As ApacheSU
834-
Dim matcher As Matcher
835-
matcher=Regex.Matcher("</*c[1-9]>",translation)
836-
Do While matcher.Find
837-
source=su.RemoveFirst(source,matcher.Match)
838-
Loop
839-
Dim matcher2 As Matcher
840-
matcher2=Regex.Matcher("</*c[1-9]>",source)
841-
If matcher2.Find Then
842-
Return True
843-
Else
844-
Return False
845-
End If
846-
End Sub
847-
848-
Sub MissingTagAddedText(translation As String,source As String) As String
849-
850-
Dim su As ApacheSU
851-
Dim matcher As Matcher
852-
matcher=Regex.Matcher("</*c[1-9]>",translation)
853-
Do While matcher.Find
854-
source=su.RemoveFirst(source,matcher.Match)
855-
Loop
856-
Dim matcher2 As Matcher
857-
matcher2=Regex.Matcher("</*c[1-9]>",source)
858-
Do While matcher2.Find
859-
translation=translation&matcher2.Match
860-
Loop
861-
Return translation
862-
End Sub
863-
864832
Sub textToListInOrder(pureText As String) As List
865833

866834
Dim result As List
@@ -1162,15 +1130,6 @@ Sub NextIsLower(XY1 As coordinate,XY2 As coordinate) As Boolean
11621130
End If
11631131
End Sub
11641132

1165-
Sub stripContent(content As String) As String
1166-
content=Regex.Replace(" +", content," ")
1167-
content=content.Replace("
","")
1168-
content=content.Replace(" "," ")
1169-
content=Regex.Replace("\n\n+",content,CRLF&CRLF)
1170-
Return content
1171-
End Sub
1172-
1173-
11741133
Sub TextFramesListOfEachSpread(spreadMap As Map) As List
11751134
Dim root As Map
11761135
root=spreadMap.Get("idPkg:Spread")
@@ -1360,15 +1319,14 @@ End Sub
13601319

13611320

13621321
Sub previewText As String
1363-
Dim text As String
1322+
Dim text As StringBuilder
1323+
text.Initialize
13641324
If Main.editorLV.Items.Size<>Main.currentProject.segments.Size Then
13651325
Return ""
13661326
End If
1327+
Dim previousStory as String
13671328
For i=Max(0,Main.currentProject.lastEntry-3) To Min(Main.currentProject.lastEntry+7,Main.currentProject.segments.Size-1)
1368-
1369-
13701329
Try
1371-
13721330
Dim p As Pane
13731331
p=Main.editorLV.Items.Get(i)
13741332
Catch
@@ -1417,11 +1375,16 @@ Sub previewText As String
14171375
translation=segmentation.removeSpacesAtBothSides(Main.currentProject.path,Main.currentProject.projectFile.Get("target"),translation,Utils.getMap("settings",Main.currentProject.projectFile).GetDefault("remove_space",True))
14181376
End If
14191377
End If
1420-
1378+
Dim story As String=bitext.Get(3)
1379+
If previousStory<>story Then
1380+
text.Append(CRLF)
1381+
previousStory=story
1382+
End If
14211383
If i=Main.currentProject.lastEntry Then
14221384
translation=$"<span id="current" name="current" >${translation}</span>"$
14231385
End If
1424-
text=text&translation
1386+
'text=text&translation
1387+
text.Append(translation)
14251388
Next
1426-
Return text
1389+
Return text.ToString
14271390
End Sub

BasicCAT/txtFilter.bas

+10-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ End Sub
5353

5454
Sub generateFile(filename As String,path As String,projectFile As Map)
5555
Dim innerfilename As String=filename
56-
Dim result As String
56+
Dim result As StringBuilder
57+
result.Initialize
5758
Dim workfile As Map
5859
Dim json As JSONParser
5960
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)
9596
End If
9697
End If
9798

98-
result=result&translation
99+
'result=result&translation
100+
result.Append(translation)
99101
Next
100102
Next
101103

102-
File.WriteString(File.Combine(path,"target"),filename,result)
104+
File.WriteString(File.Combine(path,"target"),filename,result.ToString)
103105
Main.updateOperation(filename&" generated!")
104106
End Sub
105107

@@ -236,7 +238,8 @@ Sub splitSegment(sourceTextArea As TextArea)
236238
End Sub
237239

238240
Sub previewText As String
239-
Dim text As String
241+
Dim text As StringBuilder
242+
text.Initialize
240243
If Main.editorLV.Items.Size<>Main.currentProject.segments.Size Then
241244
Return ""
242245
End If
@@ -276,7 +279,8 @@ Sub previewText As String
276279
If i=Main.currentProject.lastEntry Then
277280
translation=$"<span id="current" name="current" >${translation}</span>"$
278281
End If
279-
text=text&translation
282+
'text=text&translation
283+
text.Append(translation)
280284
Next
281-
Return text
285+
Return text.ToString
282286
End Sub

BasicCAT/xliffFilter.bas

+5-4
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,8 @@ Sub splitSegment(sourceTextArea As TextArea)
510510
End Sub
511511

512512
Sub previewText As String
513-
Dim text As String
513+
Dim text As StringBuilder
514+
text.Initialize
514515
If Main.editorLV.Items.Size<>Main.currentProject.segments.Size Then
515516
Return ""
516517
End If
@@ -553,13 +554,13 @@ Sub previewText As String
553554
Dim id As String
554555
id=extra.Get("id")
555556
If previousID<>id Then
556-
text=text&CRLF
557+
text.Append(CRLF)
557558
previousID=id
558559
End If
559560
If i=Main.currentProject.lastEntry Then
560561
translation=$"<span id="current" name="current" >${translation}</span>"$
561562
End If
562-
text=text&translation
563+
text.Append(translation)
563564
Next
564-
Return text
565+
Return text.ToString
565566
End Sub

plugins/poFilter/poFilterPlugin.bas

+11-9
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ Sub countMsgStr(path As String,filename As String) As Int
181181
End Sub
182182

183183
Sub fillPO(msgstrList As List,path As String,filename As String) As String
184-
Dim content As String
184+
Dim content As StringBuilder
185+
content.Initialize
185186

186187

187188
Dim textReader As TextReader
@@ -195,23 +196,23 @@ Sub fillPO(msgstrList As List,path As String,filename As String) As String
195196
msgstr=msgstrList.Get(0)
196197
If msgstr="" Then
197198
msgstrList.RemoveAt(0)
198-
content=content&line&CRLF
199+
content.Append(line).Append(CRLF)
199200
line=textReader.ReadLine
200201
Continue
201202
End If
202203
If msgstr.Contains("\n") Then
203204
'Log(True)
204205
msgstr=handleMultiline(msgstr)
205206
End If
206-
content=content&"msgstr "&Chr(34)&msgstr&Chr(34)&CRLF&CRLF
207+
content.Append("msgstr ").Append(Chr(34)).Append(msgstr).Append(Chr(34)).Append(CRLF).Append(CRLF)
207208
msgstrList.RemoveAt(0)
208209
Else
209-
content=content&line&CRLF
210+
content.Append(line).Append(CRLF)
210211
End If
211212
line=textReader.ReadLine
212213
Loop
213214
textReader.Close
214-
Return content
215+
Return content.ToString
215216
End Sub
216217

217218
Sub handleMultiline(text As String) As String
@@ -503,7 +504,8 @@ End Sub
503504

504505
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
505506
Log("Po preview")
506-
Dim text As String
507+
Dim text As StringBuilder
508+
text.Initialize
507509
If editorLV.Items.Size<>segments.Size Then
508510
Return ""
509511
End If
@@ -547,10 +549,10 @@ Sub previewText(editorLV As ListView,segments As List,lastEntry As Int,sourceLan
547549
id=extra.Get("id")
548550

549551
If previousID<>id Then
550-
text=text&CRLF
552+
text.Append(CRLF)
551553
previousID=id
552554
End If
553-
text=text&translation
555+
text.Append(translation)
554556
Next
555-
Return text
557+
Return text.ToString
556558
End Sub

0 commit comments

Comments
 (0)