Skip to content

Commit 7479eda

Browse files
javier-godoypaodb
authored andcommitted
perf: avoid backtracking in regex
1 parent 5254886 commit 7479eda

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

src/main/java/com/flowingcode/vaadin/addons/demo/MultiSourceCodeViewer.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ private Tab[] createTabs(List<SourceCodeTab> sourceCodeTabs) {
4343
private Tab createTab(SourceCodeTab sourceCodeTab) {
4444
String url = sourceCodeTab.getUrl();
4545
String language = sourceCodeTab.getLanguage();
46-
4746
String caption = sourceCodeTab.getCaption();
47+
48+
String filename = getFilename(url);
4849
if (caption == null) {
49-
caption = url.replaceAll(".*/", "");
50+
caption = filename;
5051
}
5152

5253
if (language == null) {
53-
String ext = url.replaceAll(".*\\.", "");
54+
String ext = getExtension(filename);
5455
switch (ext) {
5556
case "java":
5657
language = "java";
@@ -76,6 +77,16 @@ private Tab createTab(SourceCodeTab sourceCodeTab) {
7677
return tab;
7778
}
7879

80+
private String getFilename(String url) {
81+
int i = url.lastIndexOf('/');
82+
return i >= 0 ? url.substring(i + 1) : url;
83+
}
84+
85+
private String getExtension(String filename) {
86+
int i = filename.lastIndexOf('.');
87+
return i >= 0 ? filename.substring(i + 1) : filename;
88+
}
89+
7990
private void onTabSelected(Tab tab) {
8091
String url = (String) ComponentUtil.getData(tab, DATA_URL);
8192
String language = (String) ComponentUtil.getData(tab, DATA_LANGUAGE);

0 commit comments

Comments
 (0)