From 988cbb4bb0d487e616315190bdeb5892a6bd13fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Hohwiller?= Date: Thu, 5 Oct 2023 02:13:13 +0200 Subject: [PATCH] added API methods --- .../github/mmm/scanner/CharStreamScanner.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/core/src/main/java/io/github/mmm/scanner/CharStreamScanner.java b/core/src/main/java/io/github/mmm/scanner/CharStreamScanner.java index 4b5945d..61a6c4c 100644 --- a/core/src/main/java/io/github/mmm/scanner/CharStreamScanner.java +++ b/core/src/main/java/io/github/mmm/scanner/CharStreamScanner.java @@ -91,6 +91,20 @@ public interface CharStreamScanner extends TextFormatProcessor { */ String peekWhile(CharFilter filter, int maxLen); + /** + * @param stopFilter the {@link CharFilter} that decides which characters to {@link CharFilter#accept(char) accept} as + * stop characters. + * @param maxLen the maximum number of characters to peek (get as lookahead without modifying this stream). + * @return a {@link String} with the {@link #peek() peeked} characters of the given {@code maxLen} or less if a stop + * character was hit or the end-of-text has been reached before. The state of this stream remains unchanged. + * @see #readWhile(CharFilter) + * @see #skip(int) + */ + default String peekUntil(CharFilter stopFilter, int maxLen) { + + return peekWhile(stopFilter.negate(), maxLen); + } + /** * This method reads the number of {@link #next() next characters} given by {@code count} and returns them as string. * If there are less characters {@link #hasNext() available} the returned string will be shorter than {@code count} @@ -288,6 +302,22 @@ default String readUntil(CharFilter filter, boolean acceptEnd, String stop, bool */ String readUntil(CharFilter filter, boolean acceptEnd, CharScannerSyntax syntax); + /** + * @param stopFilter the {@link CharFilter} that decides which characters to {@link CharFilter#accept(char) accept} as + * stop characters. + * @param maxLength the (maximum) length of the characters to consume. + * @return the {@link String} with all consumed characters excluding the stop character. If no {@code stop} character + * was found until {@code maxLength} characters have been consumed, this method behaves like {@link #read(int) + * read(maxLength)}. + * @see #read(int) + * @see #readWhile(CharFilter, int) + * @see #peekUntil(CharFilter, int) + */ + default String readUntil(CharFilter stopFilter, int maxLength) { + + return readWhile(stopFilter.negate(), maxLength); + } + /** * This method reads all {@link #next() next characters} that are {@link CharFilter#accept(char) accepted} by the * given {@code filter}.