Sometimes we want to compare two String's characters until they mismatch;
that is, find the common prefix to the strings.
E.g. if we have a sorted list of strings, we can blot out the common
prefixes, so for "foo", "foobar", "foobaz", "foofum":
foo
---bar
-----z
---fum
Specific use cases arise from Natural Language processing, but surely it is
more generally useful, and a generalisation of the new
String.prototype.startsWith function which can detect a given prefix but not
find common prefixes.
I have trialled this function, both returning the common prefix string and
returning the length of the match, and the latter has proved more useful
when operating on a medium-large set of strings (we build the numerical
properties of the set and process that further, using 'substring' if we
actually want to know the prefix).
What to call this function?
'matchLen', 'commonPrefixLen', 'comparePrefix', 'cmpPrefix', 'equalLen'.
It is a comparison function (like 'localeCompare' and the == operator)
not an extraction function (like 'match').
Proposal:
String.prototype.cmpPrefix( matchstr )
// returns the index of the first mismatching char in this/matchstr
// thus "degroup".cmpPrefix( "detect" ) == 2
// and "foo".cmpPrefix( "bar" ) == 0
// and "foo".cmpPrefix( "foo ") == 3
Sometimes we want to compare two String's characters until they mismatch;
that is, find the common prefix to the strings.
E.g. if we have a sorted list of strings, we can blot out the common
prefixes, so for "foo", "foobar", "foobaz", "foofum":
foo
---bar
-----z
---fum
Specific use cases arise from Natural Language processing, but surely it is
more generally useful, and a generalisation of the new
String.prototype.startsWith function which can detect a given prefix but not
find common prefixes.
I have trialled this function, both returning the common prefix string and
returning the length of the match, and the latter has proved more useful
when operating on a medium-large set of strings (we build the numerical
properties of the set and process that further, using 'substring' if we
actually want to know the prefix).
What to call this function?
'matchLen', 'commonPrefixLen', 'comparePrefix', 'cmpPrefix', 'equalLen'.
It is a comparison function (like 'localeCompare' and the == operator)
not an extraction function (like 'match').
Proposal:
String.prototype.cmpPrefix( matchstr )
// returns the index of the first mismatching char in this/matchstr
// thus "degroup".cmpPrefix( "detect" ) == 2
// and "foo".cmpPrefix( "bar" ) == 0
// and "foo".cmpPrefix( "foo ") == 3
Sometimes we want to compare two String's characters until they mismatch; that is, find the common prefix to the strings.
E.g. if we have a sorted list of strings, we can blot out the common prefixes, so for "foo", "foobar", "foobaz", "foofum": foo ---bar -----z ---fum
Specific use cases arise from Natural Language processing, but surely it is more generally useful, and a generalisation of the new String.prototype.startsWith function which can detect a given prefix but not find common prefixes.
I have trialled this function, both returning the common prefix string and returning the length of the match, and the latter has proved more useful when operating on a medium-large set of strings (we build the numerical properties of the set and process that further, using 'substring' if we actually want to know the prefix).
What to call this function? 'matchLen', 'commonPrefixLen', 'comparePrefix', 'cmpPrefix', 'equalLen'. It is a comparison function (like 'localeCompare' and the == operator) not an extraction function (like 'match').
Proposal: String.prototype.cmpPrefix( matchstr ) // returns the index of the first mismatching char in this/matchstr // thus "degroup".cmpPrefix( "detect" ) == 2 // and "foo".cmpPrefix( "bar" ) == 0 // and "foo".cmpPrefix( "foo ") == 3