Wrapping a string

Talk about the Ultimate Question of Life, The Universe, and Everything
Post Reply
devster
Posts: 332
Joined: 06 Jun 2017, 22:56

Wrapping a string

Post by devster » 15 Aug 2019, 23:12

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(" ")
I only work in black and sometimes very, very dark grey. (Batman)

User avatar
rednoah
The Source
Posts: 16415
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Wrapping a string

Post by rednoah » 16 Aug 2019, 06:48

This looks pleasant to me:

Code: Select all

def wrap(s, l = '(', r = ')') {
	l + s + r
}

wrap(1)
:idea: Please read the FAQ and How to Request Help.

devster
Posts: 332
Joined: 06 Jun 2017, 22:56

Re: Wrapping a string

Post by devster » 18 Aug 2019, 09:35

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.
I only work in black and sometimes very, very dark grey. (Batman)

User avatar
rednoah
The Source
Posts: 16415
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Wrapping a string

Post by rednoah » 18 Aug 2019, 10:01

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.wrap()

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?
:idea: Please read the FAQ and How to Request Help.

devster
Posts: 332
Joined: 06 Jun 2017, 22:56

Re: Wrapping a string

Post by devster » 18 Aug 2019, 15:45

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
I only work in black and sometimes very, very dark grey. (Batman)

User avatar
rednoah
The Source
Posts: 16415
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Wrapping a string

Post by rednoah » 18 Aug 2019, 16:16

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:

Code: Select all

{
	[allOf
		{1}
		{2}
	.join()]
}
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. :D


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.
:idea: Please read the FAQ and How to Request Help.

Post Reply