# File lib/puppet/util/puppetdb/char_encoding.rb, line 118
  def self.ruby18_manually_clean_utf8(str, strip = true)

    # This is a hack to allow this code to work with either ruby 1.8 or 1.9,
    # which is useful for debugging and benchmarking.  For more info see the
    # comments in the #get_byte method below.
    @has_get_byte = str.respond_to?(:getbyte)


    i = 0
    len = str.length
    result = ""

    while i < len
      byte = get_byte(str, i)

      i += 1

      char_len = get_char_len(byte)
      case char_len
      when 0
        result.concat(Utf8ReplacementChar) unless strip
      when 1
        result << byte
      when 2..4
        ruby18_handle_multibyte_char(result, byte, str, i,  char_len, strip)
        i += char_len - 1
      else
        raise Puppet::DevError, "Unhandled UTF8 char length: '#{char_len}'"
      end

    end

    result
  end