 
   @doc """
   Returns a string where all leading and trailing Unicode whitespaces
-  has been removed.
+  have been removed.
 
   ## Examples
 

  """
  @spec rstrip(t, char) :: t
  def rstrip(string, char) when is_integer(char) do
    trim_trailing(string, <<char::utf8>>)
  end

  @doc """
  Trims all leading occurences of `match` in `string`.

  Returns the string untouched if there are no occurrences.

  ## Examples

      iex> String.trim_leading "hello world", "hello "
      "world"
      iex> String.trim_leading "hello hello world", "hello "
      "world"

  """
  def trim_leading(string, match) when is_binary(string) and is_binary(match) do
    prefix_size = byte_size(match)
    suffix_size = byte_size(string) - prefix_size
    trim_leading(string, match, prefix_size, suffix_size)
  end

  defp trim_leading(string, match, prefix_size, suffix_size) when suffix_size > 0 do
    case string do
      <<prefix::size(prefix_size)-binary, suffix::size(suffix_size)-binary>> when prefix == match ->
        trim_leading(suffix, match, prefix_size, suffix_size - prefix_size)
      _ ->
        string
    end
  end

  defp trim_leading(string, _match, _prefix_size, _suffix_size) do
    string
  end

  @doc """
  Trims all trailing occurences of `match` in `string`.

  Returns the string untouched if there are no occurrences.

  ## Examples

      iex> String.trim_trailing "hello world", " world"
      "hello"
      iex> String.trim_trailing "hello world world", " world"
      "hello"

  """
  def trim_trailing(string, match) when is_binary(string) and is_binary(match) do
    suffix_size = byte_size(match)
    prefix_size = byte_size(string) - suffix_size
    trim_trailing(string, match, prefix_size, suffix_size)
  end

  defp trim_trailing(string, match, prefix_size, suffix_size) when prefix_size > 0 do
    case string do
      <<prefix::size(prefix_size)-binary, suffix::size(suffix_size)-binary>> when suffix == match ->
        trim_trailing(prefix, match, prefix_size - suffix_size, suffix_size)
      _ ->
        string
    end
  end

  defp trim_trailing(string, _match, _prefix_size, _suffix_size) do
    string
  end

  @doc """
  Trims prefix in `string` if it matches `match`.

  Returns the string untouched if there is no match.

  ## Examples

      iex> String.trim_prefix "world", "hello "
      "world"
      iex> String.trim_prefix "hello world", "hello "
      "world"
      iex> String.trim_prefix "hello hello world", "hello "
      "hello world"

  """
  def trim_prefix(string, match) when is_binary(string) and is_binary(match) do
    prefix_size = byte_size(match)
    suffix_size = byte_size(string) - prefix_size

    case string do
      <<prefix::size(prefix_size)-binary, suffix::size(suffix_size)-binary>> when prefix == match ->
        suffix
      _ ->
        string
    end
  end

  @doc """
  Trims suffix in `string` if it matches `match`.

  Returns the string untouched if there is no match.

  ## Examples

      iex> String.trim_suffix "hello", " world"
      "hello"
      iex> String.trim_suffix "hello world", " world"
      "hello"
      iex> String.trim_suffix "hello world world", " world"
      "hello world"

  """
  def trim_suffix(string, match) when is_binary(string) and is_binary(match) do
    suffix_size = byte_size(match)
    prefix_size = byte_size(string) - suffix_size

    case string do
      <<prefix::size(prefix_size)-binary, suffix::size(suffix_size)-binary>> when suffix == match ->
        prefix
      _ ->
        string
    end

  end

  @doc ~S"""
  Returns a new string created by replacing occurences of `pattern` in
  `subject` with `replacement`.

  By default, it replaces all occurences, unless the `global` option is
  set to `false`, where it will only replace the first one

  The `pattern` may be a string or a regular expression.

