View source code
Display the source code in std/algorithm/searching.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.algorithm.searching.commonPrefix

Returns the common prefix of two ranges.

auto auto commonPrefix(alias pred, R1, R2) (
  R1 r1,
  R2 r2
)
if (isForwardRange!R1 && isInputRange!R2 && !isNarrowString!R1 && is(typeof(binaryFun!pred(r1.front, r2.front))));

auto auto commonPrefix(alias pred, R1, R2) (
  R1 r1,
  R2 r2
)
if (isNarrowString!R1 && isInputRange!R2 && is(typeof(binaryFun!pred(r1.front, r2.front))));

auto auto commonPrefix(R1, R2) (
  R1 r1,
  R2 r2
)
if (isNarrowString!R1 && isInputRange!R2 && !isNarrowString!R2 && is(typeof(r1.front == r2.front)));

auto auto commonPrefix(R1, R2) (
  R1 r1,
  R2 r2
)
if (isNarrowString!R1 && isNarrowString!R2);

Parameters

NameDescription
pred The predicate to use in comparing elements for commonality. Defaults to equality "a == b".
r1 A forward range of elements.
r2 An input range of elements.

Returns

A slice of r1 which contains the characters that both ranges start with, if the first argument is a string; otherwise, the same as the result of takeExactly(r1, n), where n is the number of elements in the common prefix of both ranges.

See Also

std.range.takeExactly

Example

writeln(commonPrefix("hello, world", "hello, there")); // "hello, "

Authors

Andrei Alexandrescu

License

Boost License 1.0.