 
   """
   @spec rstrip(t, char) :: t
-
-  def rstrip("", _char), do: ""
-
-  # Do a quick check before we traverse the whole
-  # binary. :binary.last is a fast operation (it
-  # does not traverse the whole binary).
-  def rstrip(string, char) when char in 0..127 do
-    if :binary.last(string) == char do
-      rstrip(binary_part(string, 0, byte_size(string) - 1), char)
-    else
-      string
-    end
-  end
-
   def rstrip(string, char) when is_integer(char) do
-    do_rstrip(string, "", char)
-  end
-
-  defp do_rstrip(<<char :: utf8, string :: binary>>, buffer, char) do
-    <<do_rstrip(string, <<char :: utf8, buffer :: binary>>, char) :: binary>>
-  end
-
-  defp do_rstrip(<<char :: utf8, string :: binary>>, buffer, another_char) do
-    <<buffer :: binary, char :: utf8, do_rstrip(string, "", another_char) :: binary>>
-  end
-
-  defp do_rstrip(<<>>, _, _) 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 """

  @spec split(t, pattern | Regex.t, Keyword.t) :: [t]
  def split(string, pattern, options \\ [])

  def split(string, %Regex{} = pattern, options) do
    Regex.split(pattern, string, options)
  end

  def split(string, pattern, []) when pattern != "" do
    :binary.split(string, pattern, [:global])
  end

  def split(string, pattern, options) do
    parts   = Keyword.get(options, :parts, :infinity)
    trim    = Keyword.get(options, :trim, false)
    pattern = maybe_compile_pattern(pattern)


  """
  @spec strip(t, char) :: t
  def strip(string, char) do
    rstrip(lstrip(string, char), char)
  end
