groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "K Adithyan (tech)" <adi.k....@gmail.com>
Subject Re: More Default String Methods
Date Mon, 15 Oct 2018 19:47:38 GMT
On Mon, Oct 15, 2018 at 1:41 AM MG <mgbiz@arscreat.com> wrote:

>
>
> On 14.10.2018 11:04, Jochen Theodorou wrote:
> > On 12.10.2018 21:22, adithyank wrote:
> >> As suggested by Jochen Theodorou in this
> >> link
> >> <
> http://groovy.329449.n5.nabble.com/New-DSLs-in-the-groovy-platform-itself-for-more-script-like-use-cases-td5750522.html>
>
> >>
> >> post, I have created this topic for `More Default String Methods`
> >>
> >> I would like to work in these if the community accepts this proposal
> >>
> >> //Method to return left n characters
> >> *1. static String left(String self, int chars)*
> >
> > self[0..chars]?
>
> Having a method here might be useful, but I would call it "fromLeft .
> "left" methd to me would be "all the chars left of the given char
> position", which would also be good to have, imho.
>
> ok

> >
> >> //Method to return right n characters
> >> *2. static String right(String self, int chars)*
> >
> > self[-chars..-1]?
>
> Same as "left" above (with the addition that "-chars-1" is easier to get
> wrong ;-) ).
>
> ok

>
> >
> >> //Method to return the String that is after the first occurrence of the
> >> given searchString
> >> *3. static String after(String self, String searchString)*
> >>
> >> //Method to return the String that is before the first occurrence of the
> >> given searchString
> >> *4. static String before(String self, String searchString)*
> >
> > these sound more interesting... empty String if the search string is
> > not in self?
>
> Asking mysself, how often does anyone need that, and if one does need
> it, would he rememeber/find it with that name ?
> Along the same line: Use a regex for special cases like that ?
>

I have build a (in my oragnization) dsl framework over groovy, which is
useful in analysing the log files generated by our server application.
Methods like this are very very helpful for non-developers (support staff,
QA staff, etc) for doing the log analysis, summarising data, etc.

*Ex 1: cat log.txt | gdsl --in 'line.after("Time taken = ").before("
ms").numval / 1000' | sort | uniq -c*
*Ex 2: cat log.txt | gdsl --in 'line.bw <http://line.bw>("Time taken = ", "
ms").numval / 1000' | sort | uniq -c*

Just ignore the '*--in*' option as it is part of my gdsl platform which I
have developed and see the text between the pair of single quotes. That is
valid groovy executable line.

I accept that the same can be done using regex or simple String.indexOf()
and String.substring(). But, friendly methods like this will enable
friendly text processing in the command line itself

Note : 'numval' is a friendly replacement of Groovy's 'toInteger()'...


> >
> >> //Method to return the String that is before the first '=' character
> >> *5. static String getBeforeEq(String self)*
> >
> > which is foo.before("=") if number 4 is taken... This method looks to
> > me a bit overspecialized
>
> I think it is overspecialized in any case. Better to use a class that
> parses e.g. an properties file for you, or go for a full blown parser here.
>
> Yes. Overspecialized. But, acts as friendly call in the command line log
file processing... Just my opinion. I m ok to drop this !


> >
> >> /**
> >> Splits the given String with the given separator String and returns the
> >> value at the given column Index. By
> >> default, successive occurrence of the separator String is taken are
> >> one unit
> >> and the split is performed
> >> */
> >> *6. static String column(String self, String separator, int colIndex)*
> >>
> >> /**
> >> Splits the given String with Space characters and returns the value
> >> at the
> >> given column Index.
> >> */
> >> *7. static String column(String self, int colIndex)*
> >
> > self.split(speparator)[colIndex]?
>
> Also quite a special method, but depending on implementation,
> performance of "column" method woud be much better.
>
> Yes

> >
> >> //Method to return the String that is between the pair of double quotes
> >> *8. static String bwDoubleQuotes(String line)*
> >>
> >> //Method to return the String that is between the pair of single quotes
> >> *9. static String bwSingleQuotes(String line)*
> >
> > again these look a bit overspecialized to me
>
> unquote(String s, String quoteString = '"') ?
>
> >
> >> //Method to return the String that is between the pair of given
> >> enclosure
> >> String
> >> *10. static String bw(String line, String enclosure)*
> >>
> >> //Method to return the String that is between the 2 given strings
> >> *11. static String bw(String line, String from, String to)*
>
> Is this for parsing, or is it more:
> strip(String s, String bracketString) ?
> trim(String s, String trimChars) ?
>
> "bw" is way too short / non-obvious a method name in my book.
>

It is useful for doing command line log file processing. Pls see *Ex 2 *in  my
response to 4th item in this mail.

>
> >>
> >>
> >> //Method to return the String that is between the nth pair of double
> >> quotes
> >> *12. static String bwDoubleQuotes(String line, int index)*
> >>
> >>
> >> //Method to return the String that is between the nth pair of single
> >> quotes
> >> *13. static String bwSingleQuotes(String line, int index)*
> >
> > so 10 is 11 with from=to, 12 is 10 if enclosure " and 13 is 10 with
> > enclosure '. so don`t know about 12 and 13
> >
>
True for 10 and 11.
12, 13 : If a line has multiple single or double quote enclosed strings,
this will pick the string that is in the given index.
Ex:

*def s = "  'one' some text here 'two' some text here 'three'    "*
*assert s.beSingleQuotes(2) == 'three'*


>> //Method to return the String that is between the nth pair of given
> >> enclosure String
> >> *14. static String bw(String line, String enclosure, int index)*
> >
> > this could be interesting to have
>
> Too special again, imho.
>

Yes. But very handy for non-developers...! Just my opinion !

>
> >
> >> //Method to check whether searchString exists within self String
> >> ignoring
> >> the case
> >> *15. static boolean containsIgnoreCase(String self, String
> >> searchString)*
> >>
> >> //Method to check whether the String starts with the searchString
> >> ignoring
> >> the case
> >> *16. static boolean startsWithIgnoreCase(String self, String
> >> searchString)*
> >>
> >> //Method to check whether the String ends with the searchString
> >> ignoring the
> >> case
> >> *17. static boolean endsWithIgnoreCase(String self, String
> >> searchString)*
> >
> > these could be interesting because they are actually not so easy to
> > get right and efficient in a unicode world
>
> Agree.
>
Ok

>
> Cheers,
> mg
>
>
>
>
>
>

Mime
View raw message