1193323Sed//===-- StringExtras.cpp - Implement the StringExtras header --------------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file implements the StringExtras.h header
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14202375Srdivacky#include "llvm/ADT/SmallVector.h"
15202375Srdivacky#include "llvm/ADT/STLExtras.h"
16193323Sed#include "llvm/ADT/StringExtras.h"
17193323Sedusing namespace llvm;
18193323Sed
19202375Srdivacky/// StrInStrNoCase - Portable version of strcasestr.  Locates the first
20202375Srdivacky/// occurrence of string 's1' in string 's2', ignoring case.  Returns
21202375Srdivacky/// the offset of s2 in s1 or npos if s2 cannot be found.
22202375SrdivackyStringRef::size_type llvm::StrInStrNoCase(StringRef s1, StringRef s2) {
23202375Srdivacky  size_t N = s2.size(), M = s1.size();
24202375Srdivacky  if (N > M)
25202375Srdivacky    return StringRef::npos;
26202375Srdivacky  for (size_t i = 0, e = M - N + 1; i != e; ++i)
27202375Srdivacky    if (s1.substr(i, N).equals_lower(s2))
28202375Srdivacky      return i;
29202375Srdivacky  return StringRef::npos;
30202375Srdivacky}
31202375Srdivacky
32193323Sed/// getToken - This function extracts one token from source, ignoring any
33193323Sed/// leading characters that appear in the Delimiters string, and ending the
34193323Sed/// token at any of the characters that appear in the Delimiters string.  If
35193323Sed/// there are no tokens in the source string, an empty string is returned.
36202375Srdivacky/// The function returns a pair containing the extracted token and the
37202375Srdivacky/// remaining tail string.
38202375Srdivackystd::pair<StringRef, StringRef> llvm::getToken(StringRef Source,
39202375Srdivacky                                               StringRef Delimiters) {
40193323Sed  // Figure out where the token starts.
41202375Srdivacky  StringRef::size_type Start = Source.find_first_not_of(Delimiters);
42193323Sed
43202375Srdivacky  // Find the next occurrence of the delimiter.
44202375Srdivacky  StringRef::size_type End = Source.find_first_of(Delimiters, Start);
45193323Sed
46202878Srdivacky  return std::make_pair(Source.slice(Start, End), Source.substr(End));
47193323Sed}
48193323Sed
49193323Sed/// SplitString - Split up the specified string according to the specified
50193323Sed/// delimiters, appending the result fragments to the output list.
51202375Srdivackyvoid llvm::SplitString(StringRef Source,
52202375Srdivacky                       SmallVectorImpl<StringRef> &OutFragments,
53202375Srdivacky                       StringRef Delimiters) {
54226890Sdim  std::pair<StringRef, StringRef> S = getToken(Source, Delimiters);
55226890Sdim  while (!S.first.empty()) {
56226890Sdim    OutFragments.push_back(S.first);
57226890Sdim    S = getToken(S.second, Delimiters);
58193323Sed  }
59193323Sed}
60