Table of Contents

About

Escapes the keys and values of a key-value data string and making them compliant with RFC3986.

In other words, wasKeyValueURIEscape will percent-encode the keys and values whilst leaving the key-value separators untouched (=, &). This is useful in order to be able to send data without having to worry about collisions with reserved syntax elements.

Example Usage

When called with:

string data = "hello=world&good=day sir&life=good!";
wasKeyValueURIEscape($data);

the function will return the string:

hello=world&good=day%20sir&life=good%21

Code

LSL

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2014 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
string wasKeyValueURIEscape(string data) {
    list i = llParseString2List(data, ["&", "="], []);
    list output = [];
    do {
        output += llEscapeURL(llList2String(i, 0)) + "=" + 
            llEscapeURL(llList2String(i, 1));
        i = llDeleteSubList(i, 0, 1);
    } while(llGetListLength(i) != 0);
    return llDumpList2String(output, "&");
}

PHP

###########################################################################
##  Copyright (C) Wizardry and Steamworks 2014 - License: CC BY 2.0      ##
###########################################################################
function wasKeyValueURIEscape($data) {
    preg_match_all("/(.+?)=(.+?)(&|$)/", $data, $match);
    $data = array_combine($match[1], $match[2]);
    array_walk($data,
        function(&$value, $key) {
            $value = rawurlencode($key)."=".rawurlencode($value);
        }
    );
    return implode('&', $data);
}

C#

///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2014 - License: CC BY 2.0      //
///////////////////////////////////////////////////////////////////////////
/// <summary>RFC3986 URI Escapes a string</summary>
/// <param name="data">a string to escape</param>
/// <returns>an RFC3986 escaped string</returns>
private static string wasUriEscapeDataString(string data)
{
    StringBuilder result = new StringBuilder();
    foreach (char c in data.Select(o => o))
    {
        if (char.IsLetter(c) || char.IsDigit(c))
        {
            result.Append(c);
            continue;
        }
        result.AppendFormat("%{0:X2}", (int) c);
    }
    return result.ToString();
}
 
///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2014 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
/// <summary>Escapes a dictionary's keys and values for sending as POST data.</summary>
/// <param name="data">A dictionary containing keys and values to be escaped</param>
private static Dictionary<string, string> wasKeyValueURIEscape(Dictionary<string, string> data)
{
    Dictionary<string, string> output = new Dictionary<string, string>();
    foreach (KeyValuePair<string, string> tuple in data)
    {
        output.Add(wasUriEscapeDataString(tuple.Key), wasUriEscapeDataString(tuple.Value));
    }
    return output;
}

fuss/data_structures/key-value_pairs/rfc3986/to.txt ยท Last modified: 2019/01/03 01:38 (external edit)

Access website using Tor Access website using i2p


For the copyright, license, warranty and privacy terms for the usage of this website please see the license, privacy and plagiarism pages.