Skip to content

Commit

Permalink
perf: avoid backtracking in regex
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-godoy committed Sep 25, 2023
1 parent f9a38d2 commit ac1af4b
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ private Tab[] createTabs(List<SourceCodeTab> sourceCodeTabs) {
private Tab createTab(SourceCodeTab sourceCodeTab) {
String url = sourceCodeTab.getUrl();
String language = sourceCodeTab.getLanguage();

String caption = sourceCodeTab.getCaption();

String filename = getFilename(url);
if (caption == null) {
caption = url.replaceAll(".*/", "");
caption = filename;
}

if (language == null) {
String ext = url.replaceAll(".*\\.", "");
String ext = getExtension(filename);
switch (ext) {
case "java":
language = "java";
Expand All @@ -76,6 +77,16 @@ private Tab createTab(SourceCodeTab sourceCodeTab) {
return tab;
}

private String getFilename(String url) {
int i = url.lastIndexOf('/');
return i >= 0 ? url.substring(i + 1) : url;
}

private String getExtension(String filename) {
int i = filename.lastIndexOf('.');
return i >= 0 ? filename.substring(i + 1) : filename;
}

private void onTabSelected(Tab tab) {
String url = (String) ComponentUtil.getData(tab, DATA_URL);
String language = (String) ComponentUtil.getData(tab, DATA_LANGUAGE);
Expand Down

0 comments on commit ac1af4b

Please sign in to comment.