From ef261cbbd75c03ae524b236ed1486b076501f3dd Mon Sep 17 00:00:00 2001 From: Viktor Lofgren Date: Thu, 8 Feb 2024 14:46:12 +0100 Subject: [PATCH] (search) Remove stray spaces in bang commands --- .../search/command/commands/BangCommand.java | 11 +++++------ .../search/command/commands/BangCommandTest.java | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/code/services-application/search-service/src/main/java/nu/marginalia/search/command/commands/BangCommand.java b/code/services-application/search-service/src/main/java/nu/marginalia/search/command/commands/BangCommand.java index daec60f6..5dbbec1c 100644 --- a/code/services-application/search-service/src/main/java/nu/marginalia/search/command/commands/BangCommand.java +++ b/code/services-application/search-service/src/main/java/nu/marginalia/search/command/commands/BangCommand.java @@ -53,14 +53,13 @@ public class BangCommand implements SearchCommandInterface { if (!bm.isRelativeSpaceOrInvalid(bangKey.length())) continue; - StringBuilder ret = new StringBuilder(bm.prefix().trim()); + String prefix = bm.prefix().trim(); + String suffix = bm.suffix(bangKey.length()).trim(); - if (!ret.isEmpty()) - ret.append(" "); + String ret = STR."\{prefix} \{suffix}".trim(); - ret.append(bm.suffix(bangKey.length()).trim()); - - return Optional.of(ret.toString()); + return Optional.of(ret) + .filter(s -> !s.isBlank()); } return Optional.empty(); diff --git a/code/services-application/search-service/src/test/java/nu/marginalia/search/command/commands/BangCommandTest.java b/code/services-application/search-service/src/test/java/nu/marginalia/search/command/commands/BangCommandTest.java index 047b646d..90288678 100644 --- a/code/services-application/search-service/src/test/java/nu/marginalia/search/command/commands/BangCommandTest.java +++ b/code/services-application/search-service/src/test/java/nu/marginalia/search/command/commands/BangCommandTest.java @@ -34,4 +34,20 @@ class BangCommandTest { assertEquals(match.get(), "test"); } + @Test + public void testMatchPattern2() { + var match = bangCommand.matchBangPattern("test !g", "!g"); + + assertTrue(match.isPresent()); + assertEquals(match.get(), "test"); + } + + @Test + public void testMatchPattern3() { + var match = bangCommand.matchBangPattern("hello !g world", "!g"); + + assertTrue(match.isPresent()); + assertEquals(match.get(), "hello world"); + } + } \ No newline at end of file