Skip to content

Commit

Permalink
rewrote squeeze in Rascal with reified classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Jul 17, 2024
1 parent cfa4c57 commit 1110517
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/org/rascalmpl/library/String.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module String

extend Exception;
import List;
import ParseTree;

@synopsis{All functions in this module that have a charset parameter use this as default.}
private str DEFAULT_CHARSET = "UTF-8";
Expand Down Expand Up @@ -522,8 +523,29 @@ squeeze("hello", "el");
```
}
@javaClass{org.rascalmpl.library.Prelude}
@deprecated{Use the other squeence function that accepts Rascal character classes.}
public java str squeeze(str src, str charSet);
@synopsis{Squeeze repeated occurrences of characters.}
@description{
Squeeze repeated occurrences in `src` of characters, if they are a member of `charSet`, removed.
* `src` is any string
* `&CharClass` is a character class type such as `[a-z]` (a type that is a subtype of the class of all characters `![]`)
}
@pitfalls{
* `![]` excludes the `0` character, so we can never squeeze the unicode codepoint `0`. We _can_ squeeze the number `0` of course, using `#[0-9]` for example.
}
@examples{
```rascal-shell
import String;
squeeze("hello", #[el]);
```
}
public str squeeze(str src, type[&CharClass <: ![]] _) = visit(src) {
case /<c:.><c>+/ => c
when &CharClass _ := Tree::char(charAt(c, 0))
};
@synopsis{Split a string into a list of strings based on a literal separator.}
Expand Down

0 comments on commit 1110517

Please sign in to comment.