surprising semantics for Ordered_collection_common.slice

34 views
Skip to first unread message

pel...@gmail.com

unread,
May 4, 2014, 9:28:02 PM5/4/14
to ocaml...@googlegroups.com
So I'm pretty surprised by the semantics of [slice]:

    utop # String.slice "foo" 1 1;;
    - : string = ""
    utop # String.slice "foo" 2 2;;
    - : string = ""
    utop # String.slice "foo" 0 0;;
    - : string = "foo"

This feels like an API error which is likely to lead to real bugs.  "0" looks like a poor choice of sentinel value, as it violates the reasonable expectation that [String.slice s n n] generates the empty string for all [n].

Python's slicing operator doesn't use sentinel values, and instead makes the start and end parameters optional.  Inspired by that convention, I've been using the following workaround:

    let str_slice ?(start : int option) ?(stop : int option) (s : string)
      : string =
      (* Core_string.normalize *)
      let norm s i = Core.Ordered_collection_common.normalize
          ~length_fun:String.length s i
      in
      let real_start =
        match start with
        | Some x -> norm s x
        | None   -> 0
      in
      let real_stop =
        match stop with
        | Some x -> norm s x
        | None   -> String.length s
      in
      String.sub s real_start (real_stop - real_start)

Any chance of getting a change here?  I don't like having to be mistrustful of standard library functions.

Paul


Yaron Minsky

unread,
May 4, 2014, 10:50:54 PM5/4/14
to ocaml...@googlegroups.com
Yeah, this is I think an unfortunate call, and it has been argued
persuasively internally that we should either come up with something
along the lines of what you describe, or just get rid of it.

I'm curious what others think. Is this sufficiently valuable on top
of existing string-slicing calls? Do people find the handling of
negative indices in the current call is useful? e.g., the ability to
write [String.slice "foo" 0 (-1)] and get ["fo"].

y
> --
> You received this message because you are subscribed to the Google Groups
> "ocaml-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ocaml-core+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

pel...@gmail.com

unread,
May 4, 2014, 11:27:58 PM5/4/14
to ocaml...@googlegroups.com
On Sunday, May 4, 2014 9:50:54 PM UTC-5, Yaron Minsky wrote:
I'm curious what others think.  Is this sufficiently valuable on top
of existing string-slicing calls?  Do people find the handling of
negative indices in the current call is useful?  e.g., the ability to
write [String.slice "foo" 0 (-1)] and get ["fo"].

In case it's not self-evident, my vote here would be to support slicing
with negative indices.  Having written plenty of string processing code
in Python, the value of this idiom is very clear to me.

Paul

Stanislav Artemkin

unread,
May 5, 2014, 12:08:36 PM5/5/14
to ocaml...@googlegroups.com
Personally, I like Python string slicing. Negative indices seem very practical to me.


Reply all
Reply to author
Forward
0 new messages