[PATCH] templates: add substring and string length operations

2017-07-17 Thread Rodrigo Damazio Bovendorp via Mercurial-devel
# HG changeset patch
# User Rodrigo Damazio Bovendorp 
# Date 1500072378 25200
#  Fri Jul 14 15:46:18 2017 -0700
# Node ID 0ccebbd04efbd672fc71df7f52ec243057cbed7d
# Parent  c0d8de2724ce6240d2a4241aff78ce2ee92359c2
templates: add substring and string length operations

This will allow substr(text, start, end) and strlen(text) in templates,
permitting various text formatting, such as making a (non-graphing) log line be
limited to terminal width ("substr(desc, 0, termwidth)")

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -362,6 +362,11 @@
 return ""
 return pycompat.bytestr(thing)
 
+@templatefilter('strlen')
+def stringlen(text):
+"""Any text. Turns the value into its length."""
+return len(text)
+
 @templatefilter('stripdir')
 def stripdir(text):
 """Treat the text as path and strip a directory level, if
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -1015,6 +1015,25 @@
 # i18n: "sub" is a keyword
 raise error.ParseError(_("sub got an invalid replacement: %s") % rpl)
 
+@templatefunc('substr(text, start[, end])')
+def substring(context, mapping, args):
+"""Returns a substring of the given text. Negative indices reference the 
end
+of the string."""
+if len(args) < 2 or len(args) > 3:
+  raise error.ParseError(_("substring takes 2 or 3 arguments"))
+
+text = evalstring(context, mapping, args[0])
+textlen = len(text)
+start = evalinteger(context, mapping, args[1],
+  _("start expects an integer index"))
+end = -1
+if len(args) > 2:
+  end = evalinteger(context, mapping, args[2],
+_("end expects an integer index"))
+
+# Python's [] already handles start and end boundary conditions.
+return text[start:end]
+
 @templatefunc('startswith(pattern, text)')
 def startswith(context, mapping, args):
 """Returns the value from the "text" argument
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -4011,6 +4011,35 @@
   o  line 1
  line 2
 
+Test stringlen and substring
+Full desc is "Modify, add, remove, rename".
+String idxs:  012345678901
+Reverse string idxs:  10987654321
+
+  $ hg log -R a -r . --template '{desc|strlen}\n'
+  27
+  $ hg log -R a -r . --template '{substr(desc, 5, 10)}\n'
+  y, ad
+  $ hg log -R a -r . --template '{substr(desc, 5, -10)}\n'
+  y, add, remo
+  $ hg log -R a -r . --template '{substr(desc, 5, strlen(desc) - 10)}\n'
+  y, add, remo
+  $ hg log -R a -r . --template '{substr(desc, -10, -3)}\n'
+  ve, ren
+
+Test substr with invalid indices
+
+  $ hg log -R a -r . --template '{substr(desc, 5, 200)}\n'
+  y, add, remove, rename
+  $ hg log -R a -r . --template '{substr(desc, 10, 5)}\n'
+  
+  $ hg log -R a -r . --template '{substr(desc, 100, 200)}\n'
+  
+  $ hg log -R a -r . --template '{substr(desc, -100, -50)}\n'
+  
+  $ hg log -R a -r . --template '{substr(desc, -50, -100)}\n'
+  
+
 Test bad template with better error message
 
   $ hg log -Gv -R a --template '{desc|user()}'
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


Re: [PATCH] templates: add substring and string length operations

2017-07-14 Thread Yuya Nishihara
On Fri, 14 Jul 2017 20:40:36 -0700, Martin von Zweigbergk wrote:
> On Jul 14, 2017 8:19 PM, "Yuya Nishihara"  wrote:
> 
> On Fri, 14 Jul 2017 19:50:34 -0700, Rodrigo Damazio wrote:
> > > On Fri, 14 Jul 2017 16:27:49 -0700, Rodrigo Damazio via Mercurial-devel
> > > > +@templatefunc('substr(text, start[, end])')
> > >
> > > I think substr() generally takes offset and length, not start:end range.
> >
> > Yes, I considered that the start:end (like Python has) was more powerful
> > because it allows negative numbers to reference the end, whereas offset
> and
> > length needs to be calculated in those cases. Would you like me to switch?
> 
> start and offset can be negative anyway, though length can't. The name
> 'substr()' seems confusing if it takes start:end. Can you find another name
> or switch it to take length?
> 
> Maybe Python calls it slicing? So slice() might be okay.

Sounds good to me. It could optionally support a slice over list.
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


Re: [PATCH] templates: add substring and string length operations

2017-07-14 Thread Martin von Zweigbergk via Mercurial-devel
On Jul 14, 2017 8:19 PM, "Yuya Nishihara"  wrote:

On Fri, 14 Jul 2017 19:50:34 -0700, Rodrigo Damazio wrote:
> > On Fri, 14 Jul 2017 16:27:49 -0700, Rodrigo Damazio via Mercurial-devel
> > > +@templatefunc('substr(text, start[, end])')
> >
> > I think substr() generally takes offset and length, not start:end range.
>
> Yes, I considered that the start:end (like Python has) was more powerful
> because it allows negative numbers to reference the end, whereas offset
and
> length needs to be calculated in those cases. Would you like me to switch?

start and offset can be negative anyway, though length can't. The name
'substr()' seems confusing if it takes start:end. Can you find another name
or switch it to take length?


Maybe Python calls it slicing? So slice() might be okay.

___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


Re: [PATCH] templates: add substring and string length operations

2017-07-14 Thread Yuya Nishihara
On Fri, 14 Jul 2017 19:50:34 -0700, Rodrigo Damazio wrote:
> > On Fri, 14 Jul 2017 16:27:49 -0700, Rodrigo Damazio via Mercurial-devel
> > > +@templatefunc('substr(text, start[, end])')
> >
> > I think substr() generally takes offset and length, not start:end range.
> 
> Yes, I considered that the start:end (like Python has) was more powerful
> because it allows negative numbers to reference the end, whereas offset and
> length needs to be calculated in those cases. Would you like me to switch?

start and offset can be negative anyway, though length can't. The name
'substr()' seems confusing if it takes start:end. Can you find another name
or switch it to take length?
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


Re: [PATCH] templates: add substring and string length operations

2017-07-14 Thread Rodrigo Damazio via Mercurial-devel
Thanks for the review.
We're seeing some weirdness in our script to use gmail's backends directly
- I sent v2 but it apparently never got to the list, so I'll probably try
to switch to Phabricator for v2/v3.

On Fri, Jul 14, 2017 at 7:27 PM, Yuya Nishihara  wrote:

> On Fri, 14 Jul 2017 16:27:49 -0700, Rodrigo Damazio via Mercurial-devel
> wrote:
> > On Fri, Jul 14, 2017 at 3:48 PM, Rodrigo Damazio Bovendorp <
> > rdama...@google.com> wrote:
> > > # HG changeset patch
> > > # User Rodrigo Damazio Bovendorp 
> > > # Date 1500072378 25200
> > > #  Fri Jul 14 15:46:18 2017 -0700
> > > # Node ID 0ccebbd04efbd672fc71df7f52ec243057cbed7d
> > > # Parent  c0d8de2724ce6240d2a4241aff78ce2ee92359c2
> > > templates: add substring and string length operations
>
> > > +@templatefilter('strlen')
> > > +def stringlen(text):
> > > +"""Any text. Turns the value into its length."""
> > > +return len(text)
>
> You can use "str|count" instead.
>

Ah, didn't see that one - I'll remove this then.

> > +@templatefunc('substr(text, start[, end])')
>
> I think substr() generally takes offset and length, not start:end range.
>

Yes, I considered that the start:end (like Python has) was more powerful
because it allows negative numbers to reference the end, whereas offset and
length needs to be calculated in those cases. Would you like me to switch?


smime.p7s
Description: S/MIME Cryptographic Signature
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


Re: [PATCH] templates: add substring and string length operations

2017-07-14 Thread Yuya Nishihara
On Fri, 14 Jul 2017 16:27:49 -0700, Rodrigo Damazio via Mercurial-devel wrote:
> On Fri, Jul 14, 2017 at 3:48 PM, Rodrigo Damazio Bovendorp <
> rdama...@google.com> wrote:
> > # HG changeset patch
> > # User Rodrigo Damazio Bovendorp 
> > # Date 1500072378 25200
> > #  Fri Jul 14 15:46:18 2017 -0700
> > # Node ID 0ccebbd04efbd672fc71df7f52ec243057cbed7d
> > # Parent  c0d8de2724ce6240d2a4241aff78ce2ee92359c2
> > templates: add substring and string length operations

> > +@templatefilter('strlen')
> > +def stringlen(text):
> > +"""Any text. Turns the value into its length."""
> > +return len(text)

You can use "str|count" instead.

> > +@templatefunc('substr(text, start[, end])')

I think substr() generally takes offset and length, not start:end range.
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


Re: [PATCH] templates: add substring and string length operations

2017-07-14 Thread Rodrigo Damazio via Mercurial-devel
Hmm hold off, there's an issue here.

On Fri, Jul 14, 2017 at 3:48 PM, Rodrigo Damazio Bovendorp <
rdama...@google.com> wrote:

> # HG changeset patch
> # User Rodrigo Damazio Bovendorp 
> # Date 1500072378 25200
> #  Fri Jul 14 15:46:18 2017 -0700
> # Node ID 0ccebbd04efbd672fc71df7f52ec243057cbed7d
> # Parent  c0d8de2724ce6240d2a4241aff78ce2ee92359c2
> templates: add substring and string length operations
>
> This will allow substr(text, start, end) and strlen(text) in templates,
> permitting various text formatting, such as making a (non-graphing) log
> line be
> limited to terminal width ("substr(desc, 0, termwidth)")
>
> diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
> --- a/mercurial/templatefilters.py
> +++ b/mercurial/templatefilters.py
> @@ -362,6 +362,11 @@
>  return ""
>  return pycompat.bytestr(thing)
>
> +@templatefilter('strlen')
> +def stringlen(text):
> +"""Any text. Turns the value into its length."""
> +return len(text)
> +
>  @templatefilter('stripdir')
>  def stripdir(text):
>  """Treat the text as path and strip a directory level, if
> diff --git a/mercurial/templater.py b/mercurial/templater.py
> --- a/mercurial/templater.py
> +++ b/mercurial/templater.py
> @@ -1015,6 +1015,25 @@
>  # i18n: "sub" is a keyword
>  raise error.ParseError(_("sub got an invalid replacement: %s") %
> rpl)
>
> +@templatefunc('substr(text, start[, end])')
> +def substring(context, mapping, args):
> +"""Returns a substring of the given text. Negative indices reference
> the end
> +of the string."""
> +if len(args) < 2 or len(args) > 3:
> +  raise error.ParseError(_("substring takes 2 or 3 arguments"))
> +
> +text = evalstring(context, mapping, args[0])
> +textlen = len(text)
> +start = evalinteger(context, mapping, args[1],
> +  _("start expects an integer index"))
> +end = -1
> +if len(args) > 2:
> +  end = evalinteger(context, mapping, args[2],
> +_("end expects an integer index"))
> +
> +# Python's [] already handles start and end boundary conditions.
> +return text[start:end]
> +
>  @templatefunc('startswith(pattern, text)')
>  def startswith(context, mapping, args):
>  """Returns the value from the "text" argument
> diff --git a/tests/test-command-template.t b/tests/test-command-template.t
> --- a/tests/test-command-template.t
> +++ b/tests/test-command-template.t
> @@ -4011,6 +4011,35 @@
>o  line 1
>   line 2
>
> +Test stringlen and substring
> +Full desc is "Modify, add, remove, rename".
> +String idxs:  012345678901
> +Reverse string idxs:  10987654321
> +
> +  $ hg log -R a -r . --template '{desc|strlen}\n'
> +  27
> +  $ hg log -R a -r . --template '{substr(desc, 5, 10)}\n'
> +  y, ad
> +  $ hg log -R a -r . --template '{substr(desc, 5, -10)}\n'
> +  y, add, remo
> +  $ hg log -R a -r . --template '{substr(desc, 5, strlen(desc) - 10)}\n'
> +  y, add, remo
> +  $ hg log -R a -r . --template '{substr(desc, -10, -3)}\n'
> +  ve, ren
> +
> +Test substr with invalid indices
> +
> +  $ hg log -R a -r . --template '{substr(desc, 5, 200)}\n'
> +  y, add, remove, rename
> +  $ hg log -R a -r . --template '{substr(desc, 10, 5)}\n'
> +
> +  $ hg log -R a -r . --template '{substr(desc, 100, 200)}\n'
> +
> +  $ hg log -R a -r . --template '{substr(desc, -100, -50)}\n'
> +
> +  $ hg log -R a -r . --template '{substr(desc, -50, -100)}\n'
> +
> +
>  Test bad template with better error message
>
>$ hg log -Gv -R a --template '{desc|user()}'
>


smime.p7s
Description: S/MIME Cryptographic Signature
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel