Meta-data in a string

Sometimes there is no need to create a child class to store various informations inside an object. Here I use a string property to store key and value pairs, separated by a semi-column:'' KEY1 = VALUE1 ; KEY2 = VALUE2 ; ... '' Beware to the size of the string (32ko max in Caché) and the number of elements (limited to 99 pairs by code)

The functions help store, retrieve these data.

See Useful string manipulation functions for use of these functions

GetKey
Returns the value bound to a key GetKey(str,keyname) s rightstr=$P(str,keyname_"=",2) q:rightstr="" "" q $P(rightstr,";",1)
 * Arguments:
 * str: the metadata string
 * keyname: the key
 * Returns:value of the key, or an empty string if the key is not found

SetKey
Store a key-value pair. Overwrite existing value if the key already exists SetKey(str,keyname,keyvalue) q:keyname="" str q:str="" keyname_"="_keyvalue i $F(str,keyname_"=")=0 q str_";"_keyname_"="_keyvalue s leftstr=$P(str,keyname_"=",1) s rightstr=$P($P(str,keyname_"=",2),";",2,99) i rightstr'="" s rightstr=";"_rightstr q leftstr_keyname_"="_keyvalue_rightstr
 * Arguments:
 * str: the metadata string
 * keyname: the name of the key
 * keyvalue: the value stored with the key
 * Returns:the resulting metadata string

DeleteKey
Suppress a key-value pair from the string DeleteKey(str,keyname) q:str="" "" q:keyname="" str i $F(str,keyname_"=")=0 q str s leftstr=$P(str,keyname_"=",1) s rightstr=$P($P(str,keyname_"=",2),";",2,99) i leftstr'="",rightstr="" s $E(leftstr,$L(leftstr))="" q leftstr_rightstr
 * Arguments:
 * str: the metadata string
 * keyname: the name of the key
 * Returns:the resulting metadata string

MergeKeys
Merge two metadata strings. If the same key is found in the two strings, the value of str1 is overwitten by the value of str2 MergeKeys(str1,str2) q:str1="" str2 q:str2="" str1 s id="" f { s id=$$NextKeyValue(str2,id,.keyname,.keyvalue) q:id="" s str1=$$SetKey(str1,keyname,keyvalue) }	q str1
 * Arguments:
 * str1: the first metadata string
 * str2: the second metadata string
 * Returns:the resulting metadata string

NextKeyValue
This function is like $ORDER and allow a parsing of a metadata string. See an example of use in MergeKeys(str1,str2) above NextKeyValue(str,getkeyindex,getkeykeyname,getkeykeyvalue) q:str="" "" i getkeyindex="" s getkeyindex=0 s getkeyindex=getkeyindex+1 s pair=$P(str,";",getkeyindex) q:pair="" "" s getkeykeyname=$P(pair,"=",1) s getkeykeyvalue=$P(pair,"=",2,99) q getkeyindex
 * Arguments:
 * str: the metadata string
 * getkeyindex : current position of the parser
 * getkeykeyname : name of the parsed key (by ref, use dot notation)
 * getkeykeyvalue : value of the parsed key (by ref, use dot notation)
 * Returns:next position of parser