require "git_fusion_strings/version" module GitFusionStrings # These constants are used as the default configuration for the # encode and decode methods. # GitFusion-specific character encodings to use in Perforce # This enables select characters to be used as repo names GF_CHARACTER_MAPPING = { '/' => '_0xS_', ':' => '_0xC_', } GF_DEFAULT_ILLEGAL_CHARS = GF_CHARACTER_MAPPING.keys # Characters that are permitted in usernames in Perforce # but render URI impossible to decode URI_RESERVED_CHARACTERS = ['/', '\\', ':', '?', '=', '%', ':', '#'] # Encodes a string for Git Fusion use def self.encode(str, illegal_characters: GF_DEFAULT_ILLEGAL_CHARS) str.chars.map do |ch| if illegal_characters.include?(ch) GF_CHARACTER_MAPPING[ch] else ch end end.join('') end # Decodes a string coming from GitFusion encoding def self.decode(str, illegal_characters: GF_DEFAULT_ILLEGAL_CHARS) escape_map = Hash[illegal_characters.map{ |c| [c, GF_CHARACTER_MAPPING[c]] }] escape_map.each do |ch, escaped| str = str.gsub(escaped, ch) end str end end