Greets,

Many programming languages provide starts-with and ends-with functionality for
strings.  Typical implementations return a boolean and support accepting a
single string prefix/suffix as an argument.  Most provide some additional
functionality as well.

Ruby's methods, String#start_with? and String#end_with? both allow the user to
supply multiple strings; if any one of the candidates matches, the test
succeeds.

    http://rubydoc.info/stdlib/core/String#start_with%3F-instance_method
    http://rubydoc.info/stdlib/core/String#end_with%3F-instance_method

In addition to the base functionality, Java supports an overloaded
startsWith() which takes an `int toffset` at which to begin the search.

    
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith%28java.lang.String%29
    
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith%28java.lang.String,%20int%29
    
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#endsWith%28java.lang.String%29

Like Ruby, Python's str.startswith() supports supplying multiple candidate
strings for matching.  Like Java, it supports an offset as a second optional
argument, `start` (which defaults to 0), at which to begin the search.  In
addition, Python supports a third argument, `end`, at which to stop comparing.

Python's str.endswith() likewise supports optional `start` and `end`
parameters.

    http://docs.python.org/3/library/stdtypes.html#str.startswith
    http://docs.python.org/3/library/stdtypes.html#str.endswith

C# provides extensibility by accepting additional arguments which control how
the comparison is made.

    http://msdn.microsoft.com/en-us/library/system.string.startswith.aspx
    http://msdn.microsoft.com/en-us/library/system.string.endswith.aspx

Objective-C's NSString provides only the base functionality, though under the
names `hasPrefix:` and `hasSuffix:`.

    
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/hasPrefix:
    
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/hasSuffix:

Go's `strings` package supplies `HasPrefix` and `HasSuffix`, which provide
only the base functionality.

    http://golang.org/pkg/strings/#HasPrefix
    http://golang.org/pkg/strings/#HasSuffix

...

After reviewing all these varied APIs, it seems to me that providing the base
functionality suffices.

    /** Tests whether the String starts with the content of `prefix`.
     */
    bool
    Starts_With(String *self, String *prefix);

    /** Tests whether the String ends with the content of `suffix`.
     */
    bool
    Ends_With(String *self, String *suffix);

Rationale:

*   The base functionality is common to every language surveyed.
*   Testing multiple candidate strings a la Ruby/Python is easy enough to
    achieve with a for-loop which iterates over an array.
*   It makes more sense to add an offset to Find() than to Starts_With().  If
    we do that, Find() can be used to simulate the functionality of the offset
    parameter supported by Python and Java.
*   The functionality that C# provides is too elaborate for the core
    Clownfish runtime.  If someone wants it, they can create a library of
    utility functions.

Marvin Humphrey

Reply via email to