# File lib/rbreadline.rb, line 1317
  def compute_lcd_of_matches(match_list, matches, text)
    # If only one match, just use that.  Otherwise, compare each
    #   member of the list with the next, finding out where they
    #   stop matching.
    if (matches == 1)
      match_list[0] = match_list[1]
      match_list[1] = nil
      return 1
    end

    i = 1
    low = 100000
    while(i<matches)
      if (@_rl_completion_case_fold)
        si = 0
        while((c1 = _rl_to_lower(match_list[i][si])) &&
              (c2 = _rl_to_lower(match_list[i + 1][si])))
          if !@rl_byte_oriented
            if(!_rl_compare_chars(match_list[i],si,match_list[i+1],si))
              break
            elsif ((v = _rl_get_char_len(match_list[i][si..-1])) > 1)
              si += v - 1
            end
          else
            break if (c1 != c2)
          end
          si += 1
        end
      else
        si = 0
        while((c1 = match_list[i][si]) &&
              (c2 = match_list[i + 1][si]))
          if !@rl_byte_oriented
            if(!_rl_compare_chars(match_list[i],si,match_list[i+1],si))
              break
            elsif ((v = _rl_get_char_len(match_list[i][si..-1])) > 1)
              si += v - 1
            end
          else
            break if (c1 != c2)
          end
          si += 1
        end
      end

      if (low > si)
        low = si
      end
      i += 1
    end

    # If there were multiple matches, but none matched up to even the
    #   first character, and the user typed something, use that as the
    #   value of matches[0].
    if (low == 0 && text && text.length>0 )
      match_list[0] = text.dup
    else
      # XXX - this might need changes in the presence of multibyte chars

      # If we are ignoring case, try to preserve the case of the string
      # the user typed in the face of multiple matches differing in case.
      if (@_rl_completion_case_fold)

        # We're making an assumption here:
        #  IF we're completing filenames AND
        #     the application has defined a filename dequoting function AND
        #     we found a quote character AND
        #     the application has requested filename quoting
        #  THEN
        #     we assume that TEXT was dequoted before checking against
        #     the file system and needs to be dequoted here before we
        #     check against the list of matches
        #  FI
        if (@rl_filename_completion_desired &&
            @rl_filename_dequoting_function &&
            @rl_completion_found_quote &&
            @rl_filename_quoting_desired)

          dtext = send(@rl_filename_dequoting_function,text, @rl_completion_quote_character)
          text = dtext
        end

        # sort the list to get consistent answers.
        match_list = [match_list[0]] + match_list[1..-1].sort

        si = text.length
        if (si <= low)
          for i in 1 .. matches
            if match_list[i][0,si] == text
              match_list[0] = match_list[i][0,low]
              break
            end
            # no casematch, use first entry
            if (i > matches)
              match_list[0] = match_list[1][0,low]
            end
          end
        else
          # otherwise, just use the text the user typed.
          match_list[0] = text[0,low]
        end
      else
        match_list[0] = match_list[1][0,low]
      end
    end

    return matches
  end