def buffer_split_by_interpolation(string, escape = true)
      # Interpolate variables in text (#{variable}).
      # Split the text into multiple dynamic and static parts.
      begin
        case string
        when /\A\\\#\{/
          # Escaped interpolation
          buffer_freeze '#{'
          string = $'
        when /\A#\{((?>[^{}]|(\{(?>[^{}]|\g<1>)*\}))*)\}/
          # Interpolation
          string = $'
          code = $1

          # escape = code !~ /\A\{.*\}\Z/
          if escape
            buffer_escape code
          else
            buffer code
          end
        when /\A([\\#]?[^#\\]*([#\\][^\\#\{][^#\\]*)*)/
          string_remaining = $'
          string_current = $&

          # Static text
          if escape && string_current =~ Utils::ESCAPE_HTML_PATTERN
            buffer_escape string_current.inspect
          else
            buffer_freeze string_current
          end

          string = string_remaining
        end
      end until string.empty?
    end
def buffer_split_by_interpolation(string, escape = true)
      # Interpolate variables in text (#{variable}).
      # Split the text into multiple dynamic and static parts.
      begin
        case string
        when /\A\\\#\{/
          # Escaped interpolation
          buffer_freeze '#{'
          string = $'
        when /\A#\{((?>[^{}]|(\{(?>[^{}]|\g<1>)*\}))*)\}/
          # Interpolation
          string = $'
          code = $1

          # escape = code !~ /\A\{.*\}\Z/
          if escape
            buffer_escape code
          else
            buffer code
          end
        when /\A([\\#]?[^#\\]*([#\\][^\\#\{][^#\\]*)*)/
          string_remaining = $'
          string_current = $&

          # Static text
          if escape && string_current =~ Utils::ESCAPE_HTML_PATTERN
            buffer_escape string_current.inspect
          else
            buffer_freeze string_current
          end

          string = string_remaining
        end
      end until string.empty?
    end
Split a string by its interpolation, then check if it really needs to be
 escaped or not. Huge performance boost!

 @param string [String] Input string
 @param escape [Boolean] Escape string

 @ref slim-lang Thank you for the interpolation code