/// Split a string
/// Based on: https://stackoverflow.com/questions/32376461/how-to-split_anthology-string-into-two-lists-of-numbers-in-sass
///
/// @param {String} $string - The string to split.
/// @param {String} $delimiter - The delimiter to split on.
/// @return {List} - A list containing the remaining strings after splitting.
@function anthology-str-split($string, $delimiter)
{
  $result: ();
  $index: str-index($string, $delimiter);

  @while $index != null
  {
    // Find the substring at the next instance of $index
    $item: str-slice($string, 1, $index - 1);
    $result: append($result, $item);

    // Remove the substring and update $index
    $string: str-slice($string, $index + 1);
    $index: str-index($string, $delimiter);
  }

  // Add the remaining string to list (the last item)
  $result: append($result, $string);

  @return $result;
}

/// Checks if a substring exists somewhere in a parent string.
///
/// @param {String} $string - The string we are searching in.
/// @param {String} $substring - The string we are searching for.
/// @return {Bool} - The truthiness of the substrings's existence in the parent string.
@function anthology-str-contains($string, $substring)
{
  @return str-index($string, $substring) != null;
}

/// Checks if a substring exists at the beginning of a parent string.
///
/// @param {String} $string - The string we are searching in.
/// @param {String} $substring - The string we are searching for.
/// @return {Bool} - The truthiness of the substring's existence in the parent string.
@function anthology-str-starts-with($string, $substring)
{
  @return str-index($string, $substring) == 1;
}

/// _anthology-stringify an arbitrary number of values.
///
/// @param {ArgList} $values - A list of values to _anthology-stringify.
/// @return {String} - The resulting string.
@function anthology-stringify($values...)
{
  @return '#{$values}';
}

@function anthology-str-replace($string, $search, $replace: '') {
  $index: str-index($string, $search);

  @if $index {
    @return str-slice($string, 1, $index - 1) + $replace + A(str-replace, str-slice($string, $index + str-length($search)), $search, $replace);
  }

  @return $string;
}

$_anthology-escape-chars: (
  '!', '"', '#', '$', '%','&', "'", '(', ')', '*',

  '+', ',', '.', '/', ':', ';', '<', '=', '>',

  '?', '@', '[', ']', '^', '`', '{', '|', '}', '~'
);

/// Escapes
@function anthology-str-escape($string, $escape-multiplicity: 1) {
  $result: $string;
  $escape: '';

  // Create a string of '\\' escapes at the appropriate length.
  @for $i from 1 through $escape-multiplicity
  {
    $escape: '#{$escape}\\';
  }

  // Escape the listed characters
  @each $c in $_anthology-escape-chars
  {
    $result: A(str-replace, $result, $c,'#{$escape}#{$c}');
  }

  @return $result;
}
