Page 1 of 1
Wrapping a string
Posted: 15 Aug 2019, 23:12
by devster
What would be a groovy way to conditionally wrap a string given the wrapping characters?
Having a method/closure would seem a good way to do it, but I'm struggling to write it.
Code: Select all
String.metaClass.wrap { l, r ->
l + delegate + r
}
assert "mycustomstring".wrap("(", ")") == "(mycustomstring)"
// example use
allOf
{ if (sc > 1) s00e00 }
{ sc > 1 ? absolute.wrap("(", ")") : absolute }
.join(" ")
Re: Wrapping a string
Posted: 16 Aug 2019, 06:48
by rednoah
This looks pleasant to me:
Code: Select all
def wrap(s, l = '(', r = ')') {
l + s + r
}
wrap(1)
Re: Wrapping a string
Posted: 18 Aug 2019, 09:35
by devster
Thank you, implemented.
I would also submit for consideration the following:
Code: Select all
/**
* Wraps a string with specified characters, uses "(" and ")" by default.
*
* e.g assert "Doctor Who".wrap() == "(Doctor Who)"
*
* @param left Character used on the left of the string, "(" by default
* @param right Character used on the right of the string, ")" by default
* @return String wrapped with the specified Characters
* @since 4.8.6
*/
public static String wrap(String self, Character left = "(", Character right = ")") {
return left + self + right
}
as an addition to the methods of
https://www.filebot.net/docs/api/src-ht ... thods.html
There's âž” between the examples (e.g.) in the source page, not sure what that's meant to be.
Re: Wrapping a string
Posted: 18 Aug 2019, 10:01
by rednoah
1.
I'm inclined not to add this, since it seems to me it's a complicated non-standard way of doing something simple and standardized, also "wrap" usually refers to newlines, so it adds confusion there as well:
Code: Select all
def word = "Hello"
"(" + word + ")"
Perhaps adding logic would give this reason to exist?
Code: Select all
word = word.trim()
if (word.isEmpty()) throw new Exception("Empty String")
return "(" + word + ")"
What was your reasoning for adding such a function?
2.
devster wrote: ↑18 Aug 2019, 09:35
There's âž” between the examples (e.g.) in the source page, not sure what that's meant to be.
Which line? Looks good to me. What encoding is your browser using to display the page?
Re: Wrapping a string
Posted: 18 Aug 2019, 15:45
by devster
No problem, just a proposal.
1. several bits of my format use something like:
Code: Select all
{
allOf
{" ["}
{ allOf{ vf }{ vc }{ if (bitdepth > 8) "$bitdepth-bit"}.join(" ") }
{ "some long audio stuff" }
{"]"}
.join()
}
which I would simplify like so:
Code: Select all
{
allOf
{ allOf{ vf }{ vc }{ if (bitdepth > 8) "$bitdepth-bit"}.join(" ") }
{ "some long audio stuff" }
.join().wrap("[", "]")
}
It's really not an issue however, as I can define it in my format.
I did change the name to "surround", inspired by
https://github.com/tpope/vim-surround
2. I'm using Chrome on MacOS, nothing special, this is what I see:
https://snipboard.io/PNgwUf.jpg
Re: Wrapping a string
Posted: 18 Aug 2019, 16:16
by rednoah
1.
In this particular instance, you can actually just use List syntax [...] since the String representation of List just so happen to do exactly what we want:
I'll add this for you with the next revision:
Code: Select all
{
allOf
{1}
{2}
.joining('', '[', ']')
}
Since
java.util.stream.Collectors.joining() has the same API, this should be at least somewhat intuitive.
2.
That's supposed to be a ➔ character. Strange. I'm on macOS / Chrome right now as well. The javadoc output doesn't seem to add any encoding meta tags, but it should default to UTF-8 nevertheless.