robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1if exists ( 'g:loaded_msgpack_autoload' ) 2finish 3endif 4let g: loaded_msgpack_autoload = 1 5 6"" 7" Check that given value is an integer. Respects |msgpack-special-dict|. 8function msgpack#is_int ( v ) abort 9return type ( a: v ) == type ( 0 ) || ( 10 \type ( a: v ) == type ({}) && get ( a: v , '_TYPE' ) is# v: msgpack_types . integer ) 11endfunction 12 13"" 14" Check that given value is an unsigned integer. Respects 15" |msgpack-special-dict|. 16function msgpack#is_uint ( v ) abort 17return msgpack#is_int ( a: v ) && ( type ( a: v ) == type ( 0 ) 18 \?a :v >= 0 19 \: a:v._VAL[0] > 0) 20endfunction 21 22"" 23" True if s:msgpack_init_python() function was already run. 24let s: msgpack_python_initialized = 0 25 26"" 27" Cached return of s:msgpack_init_python() used when 28" s:msgpack_python_initialized is true. 29let s: msgpack_python_type = 0 30 31"" 32" Create Python functions that are necessary for work. Also defines functions 33" s:msgpack_dict_strftime(format, timestamp) and s:msgpack_dict_strptime(format, 34" string). 35" 36" @return Zero in case no Python is available, empty string if Python-2 is 37" available and string `"3"` if Python-3 is available. 38function s: msgpack_init_python () abort 39if s: msgpack_python_initialized 40return s: msgpack_python_type 41endif 42let s: msgpack_python_initialized = 1 43for suf in ( has ( 'win32' ) ? [ '3' ] : [ '' , '3' ]) 44try 45execute 'python' . suf 46 \. "\n" 47 \. "def shada_dict_strftime():\n" 48 \. " import datetime\n" 49 \. " import vim\n" 50 \. " fmt = vim.eval('a:format')\n" 51 \. " timestamp = vim.eval('a:timestamp')\n" 52 \. " timestamp = [int(v) for v in timestamp['_VAL']]\n" 53 \. " timestamp = timestamp[0] * (timestamp[1] << 62\n" 54 \. " | timestamp[2] << 31\n" 55 \. " | timestamp[3])\n" 56 \. " time = datetime.datetime.fromtimestamp(timestamp)\n" 57 \. " return time.strftime(fmt)\n" 58 \. "def shada_dict_strptime():\n" 59 \. " import calendar\n" 60 \. " import datetime\n" 61 \. " import vim\n" 62 \. " fmt = vim.eval('a:format')\n" 63 \. " timestr = vim.eval('a:string')\n" 64 \. " timestamp = datetime.datetime.strptime(timestr, fmt)\n" 65 \. " try:\n" 66 \. " timestamp = int(timestamp.timestamp())\n" 67 \. " except:\n" 68 \. " try:\n" 69 \. " timestamp = int(timestamp.strftime('%s'))\n" 70 \. " except:\n" 71 \. " timestamp = calendar.timegm(timestamp.utctimetuple())\n" 72 \. " if timestamp > 2 ** 31:\n" 73 \. " tsabs = abs(timestamp)\n" 74 \. " return ('{\"_TYPE\": v:msgpack_types.integer,'\n" 75 \. " + '\"_VAL\": [{sign},{v1},{v2},{v3}]}').format(\n" 76 \. " sign=(1 if timestamp >= 0 else -1),\n" 77 \. " v1=((tsabs >> 62) & 0x3),\n" 78 \. " v2=((tsabs >> 31) & (2 ** 31 - 1)),\n" 79 \. " v3=(tsabs & (2 ** 31 - 1)))\n" 80 \. " else:\n" 81 \. " return str(timestamp)\n" 82execute "function s:msgpack_dict_strftime(format, timestamp) abort\n" 83 \. " return py" . suf . "eval('shada_dict_strftime()')\n" 84 \. "endfunction\n" 85 \. "function s:msgpack_dict_strptime(format, string)\n" 86 \. " return eval(py" . suf . "eval('shada_dict_strptime()'))\n" 87 \. "endfunction\n" 88let s: msgpack_python_type = suf 89return suf 90catch 91continue 92endtry 93endfor 94 95"" 96" strftime() function for |msgpack-special-dict| values. 97" 98" @param[in] format String according to which time should be formatted. 99" @param[in] timestamp Timestamp (seconds since epoch) to format. 100" 101" @return Formatted timestamp. 102" 103" @warning Without +python or +python3 this function does not work correctly. 104" The Vimscript code contains “reference” implementation which does 105" not really work because of precision loss. 106function s: msgpack_dict_strftime ( format , timestamp ) 107return msgpack#strftime ( a: format , + msgpack#int_dict_to_str ( a: timestamp )) 108endfunction 109 110"" 111" Function that parses given string according to given format. 112" 113" @param[in] format String according to which string was formatted. 114" @param[in] string Time formatted according to format. 115" 116" @return Timestamp. 117" 118" @warning Without +python or +python3 this function is able to work only with 119" 31-bit (32-bit signed) timestamps that have format 120" `%Y-%m-%dT%H:%M:%S`. 121function s: msgpack_dict_strptime ( format , string ) 122let fmt = '%Y-%m-%dT%H:%M:%S' 123if a: format isnot# fmt 124throw 'notimplemented-format:Only ' . fmt . ' format is supported' 125endif 126let match = matchlist ( a: string , 127 \'\v\C^(\d+)\-(\d+)\-(\d+)T(\d+)\:(\d+)\:(\d+)$' ) 128if empty ( match ) 129throw 'invalid-string:Given string does not match format ' . a: format 130endif 131call map ( match , 'str2nr(v:val, 10)' ) 132let [ year , month , day , hour , minute , second ] = match [ 1 : 6 ] 133" Bisection start and end: 134" 135" Start: 365 days in year, 28 days in month, -14 hours tz offset. 136let bisect_ts_start = ((((( year - 1970 ) * 365 137 \+ ( month - 1 ) * 28 138 \+ ( day - 1 )) * 24 139 \+ hour - 14 ) * 60 140 \+ minute ) * 60 141 \+ second ) 142if bisect_ts_start < 0 143let bisect_ts_start = 0 144endif 145let start_string = strftime ( fmt , bisect_ts_start ) 146if start_string is# a: string 147return bisect_ts_start 148endif 149" End: 366 days in year, 31 day in month, +14 hours tz offset. 150let bisect_ts_end = ((((( year - 1970 ) * 366 151 \+ ( month - 1 ) * 31 152 \+ ( day - 1 )) * 24 153 \+ hour + 14 ) * 60 154 \+ minute ) * 60 155 \+ second ) 156let end_string = strftime ( fmt , bisect_ts_end ) 157if end_string is# a: string 158return bisect_ts_end 159endif 160if start_string ># end_string 161throw 'internal-start-gt:Internal error: start > end' 162endif 163if start_string is# end_string 164throw printf ( 'internal-start-eq:Internal error: ' 165 \. 'start(%u)==end(%u), but start(%s)!=string(%s)' , 166 \bisect_ts_start , bisect_ts_end , 167 \string ( start_string ), string ( a: string )) 168endif 169if start_string ># a: string 170throw 'internal-start-string:Internal error: start > string' 171endif 172if end_string <# a: string 173throw 'internal-end-string:Internal error: end < string' 174endif 175while 1 176let bisect_ts_middle = ( bisect_ts_start / 2 ) + ( bisect_ts_end / 2 ) 177let middle_string = strftime ( fmt , bisect_ts_middle ) 178if a: string is# middle_string 179return bisect_ts_middle 180elseif a: string ># middle_string 181if bisect_ts_middle == bisect_ts_start 182let bisect_ts_start += 1 183else 184let bisect_ts_start = bisect_ts_middle 185endif 186else 187if bisect_ts_middle == bisect_ts_end 188let bisect_ts_end -= 1 189else 190let bisect_ts_end = bisect_ts_middle 191endif 192endif 193if bisect_ts_start >= bisect_ts_end 194throw 'not-found:Unable to find timestamp' 195endif 196endwhile 197endfunction 198 199return 0 200endfunction 201 202"" 203" Wrapper for strftime() that respects |msgpack-special-dict|. May actually use 204" non-standard strftime() implementations for |msgpack-special-dict| values. 205" 206" @param[in] format Format string. 207" @param[in] timestamp Formatted timestamp. 208function msgpack#strftime ( format , timestamp ) abort 209if type ( a: timestamp ) == type ({}) 210call s: msgpack_init_python () 211return s: msgpack_dict_strftime ( a: format , a: timestamp ) 212else 213return strftime ( a: format , a: timestamp ) 214endif 215endfunction 216 217"" 218" Parse string according to the format. 219" 220" Requires +python available. If it is not then only supported format is 221" `%Y-%m-%dT%H:%M:%S` because this is the format used by ShaDa plugin. Also in 222" this case bisection will be used (timestamps tried with strftime() up until 223" result matches the string) and only 31-bit (signed 32-bit: with negative 224" timestamps being useless this leaves 31 bits) timestamps will be supported. 225" 226" @param[in] format Time format. 227" @param[in] string Parsed time string. Must match given format. 228" 229" @return Timestamp. Possibly as |msgpack-special-dict|. 230function msgpack#strptime ( format , string ) abort 231call s: msgpack_init_python () 232return s: msgpack_dict_strptime ( a: format , a: string ) 233endfunction 234 235let s: MSGPACK_HIGHEST_BIT = 1 236let s: MSGPACK_HIGHEST_BIT_NR = 0 237while s: MSGPACK_HIGHEST_BIT * 2 > 0 238let s: MSGPACK_HIGHEST_BIT = s: MSGPACK_HIGHEST_BIT * 2 239let s: MSGPACK_HIGHEST_BIT_NR += 1 240endwhile 241 242"" 243" Shift given number by given amount of bits 244function s: shift ( n , s ) abort 245if a: s == 0 246return a: n 247elseif a: s < 0 248let ret = a: n 249for _ in range ( - a: s ) 250let ret = ret / 2 251endfor 252return ret 253else 254let ret = a: n 255for i in range ( a: s ) 256let new_ret = ret * 2 257if new_ret < ret 258" Overflow: remove highest bit 259let ret = xor ( s: MSGPACK_HIGHEST_BIT , ret ) * 2 260endif 261let ret = new_ret 262endfor 263return ret 264endif 265endfunction 266 267let s: msgpack_mask_cache = { 268 \s: MSGPACK_HIGHEST_BIT_NR : s: MSGPACK_HIGHEST_BIT - 1 } 269 270"" 271" Apply a mask where first m bits are ones and other are zeroes to a given 272" number 273function s: mask1 ( n , m ) abort 274if a: m > s: MSGPACK_HIGHEST_BIT_NR + 1 275let m = s: MSGPACK_HIGHEST_BIT_NR + 1 276else 277let m = a: m 278endif 279if ! has_key ( s: msgpack_mask_cache , m ) 280let p = 0 281for _ in range ( m ) 282let p = p * 2 + 1 283endfor 284let s: msgpack_mask_cache [ m ] = p 285endif 286return and ( a: n , s: msgpack_mask_cache [ m ]) 287endfunction 288 289"" 290" Convert |msgpack-special-dict| that represents integer value to a string. Uses 291" hexadecimal representation starting with 0x because it is the easiest to 292" convert to. 293function msgpack#int_dict_to_str ( v ) abort 294let v = a: v . _VAL 295" 64-bit number: 296" 0000000001111111111222222222233333333334444444444555555555566666 297" 1234567890123456789012345678901234567890123456789012345678901234 298" Split in _VAL: 299" 0000000001111111111222222222233 3333333344444444445555555555666 66 300" 1234567890123456789012345678901 2345678901234567890123456789012 34 301" Split by hex digits: 302" 0000 0000 0111 1111 1112 2222 2222 2333 3333 3334 4444 4444 4555 5555 5556 6666 303" 1234 5678 9012 3456 7890 1234 5678 9012 3456 7890 1234 5678 9012 3456 7890 1234 304" 305" Total split: 306" _VAL[3] _VAL[2] _VAL[1] 307" ______________________________________ _______________________________________ __ 308" 0000 0000 0111 1111 1112 2222 2222 233 3 3333 3334 4444 4444 4555 5555 5556 66 66 309" 1234 5678 9012 3456 7890 1234 5678 901 2 3456 7890 1234 5678 9012 3456 7890 12 34 310" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^ 311" g4 g3 g2 g1 312" ********************************** *** * ********************************** ** ** 313" 1 2 3 4 5 6 314" 1: s:mask1(v[3], 28): first 28 bits of _VAL[3] 315" 2: s:shift(v[3], -28): last 3 bits of _VAL[3] 316" 3: s:mask1(v[2], 1): first bit of _VAL[2] 317" 4: s:mask1(s:shift(v[2], -1), 28): bits 2 .. 29 of _VAL[2] 318" 5: s:shift(v[2], -29): last 2 bits of _VAL[2] 319" 6: s:shift(v[1], 2): _VAL[1] 320let g4 = printf ( '%07x' , s: mask1 ( v [ 3 ], 28 )) 321let g3 = printf ( '%01x' , or ( s: shift ( v [ 3 ], -28 ), s: shift ( s: mask1 ( v [ 2 ], 1 ), 3 ))) 322let g2 = printf ( '%07x' , s: mask1 ( s: shift ( v [ 2 ], -1 ), 28 )) 323let g1 = printf ( '%01x' , or ( s: shift ( v [ 2 ], -29 ), s: shift ( v [ 1 ], 2 ))) 324return (( v [ 0 ] < 0 ? '-' : '' ) . '0x' . g1 . g2 . g3 . g4 ) 325endfunction 326 327"" 328" True boolean value. 329let g: msgpack#true = { '_TYPE' : v: msgpack_types . boolean , '_VAL' : 1 } 330lockvar ! g:msgpack#true 331 332"" 333" False boolean value. 334let g: msgpack#false = { '_TYPE' : v: msgpack_types . boolean , '_VAL' : 0 } 335lockvar ! g:msgpack#false 336 337"" 338" NIL value. 339let g: msgpack#nil = { '_TYPE' : v: msgpack_types . nil , '_VAL' : 0 } 340lockvar ! g:msgpack#nil 341 342"" 343" Deduce type of |msgpack-special-dict|. 344" 345" @return zero if given dictionary is not special or name of the key in 346" v:msgpack_types dictionary. 347function msgpack#special_type ( v ) abort 348if type ( a: v ) != type ({}) || ! has_key ( a: v , '_TYPE' ) 349return 0 350endif 351for [ k , v ] in items ( v: msgpack_types ) 352if a: v . _TYPE is v 353return k 354endif 355endfor 356return 0 357endfunction 358 359"" 360" Mapping that maps type() output to type names. 361let s: MSGPACK_STANDARD_TYPES = { 362 \type ( 0 ): 'integer' , 363 \type ( 0.0 ): 'float' , 364 \type ( '' ): 'string' , 365 \type ([]): 'array' , 366 \type ({}): 'map' , 367 \type ( v: true ): 'boolean' , 368 \type ( v: null ): 'nil' , 369\} 370 371"" 372" Deduce type of one of items returned by msgpackparse(). 373" 374" @return Name of a key in v:msgpack_types. 375function msgpack#type ( v ) abort 376let special_type = msgpack#special_type ( a: v ) 377if special_type is 0 378return s: MSGPACK_STANDARD_TYPES [ type ( a: v )] 379endif 380return special_type 381endfunction 382 383"" 384" Dump nil value. 385function s: msgpack_dump_nil ( v ) abort 386return 'NIL' 387endfunction 388 389"" 390" Dump boolean value. 391function s: msgpack_dump_boolean ( v ) abort 392return ( a: v is v: true || ( a: v isnot v: false && a: v . _VAL )) ? 'TRUE' : 'FALSE' 393endfunction 394 395"" 396" Dump integer msgpack value. 397function s: msgpack_dump_integer ( v ) abort 398if type ( a: v ) == type ({}) 399return msgpack#int_dict_to_str ( a: v ) 400else 401return string ( a: v ) 402endif 403endfunction 404 405"" 406" Dump floating-point value. 407function s: msgpack_dump_float ( v ) abort 408return substitute ( string ( type ( a: v ) == type ({}) ? a: v . _VAL : a: v ), 409 \'\V\^\(-\)\?str2float(''\(inf\|nan\)'')\$' , '\1\2' , '' ) 410endfunction 411 412"" 413" Dump |msgpack-special-dict| that represents a string. If any additional 414" parameter is given then it dumps binary string. 415function s: msgpack_dump_string ( v ) abort 416if type ( a: v ) == type ({}) 417let val = a: v 418else 419let val = { '_VAL' : split ( a: v , "\n" , 1 )} 420end 421 422let ret = [ '"' ] 423for v in val . _VAL 424call add ( 425 \ret , 426 \substitute ( 427 \substitute ( v , '["\\]' , '\\\0' , 'g' ), 428 \'\n' , '\\0' , 'g' )) 429call add ( ret , '\n' ) 430endfor 431let ret [ -1 ] = '"' 432return join ( ret , '' ) 433endfunction 434 435"" 436" Dump array value. 437function s: msgpack_dump_array ( v ) abort 438let val = type ( a: v ) == type ({}) ? a: v . _VAL : a: v 439return '[' . join ( map ( val [:], 'msgpack#string(v:val)' ), ', ' ) . ']' 440endfunction 441 442"" 443" Dump dictionary value. 444function s: msgpack_dump_map ( v ) abort 445let ret = [ '{' ] 446if msgpack#special_type ( a: v ) is 0 447for [ k , v ] in items ( a: v ) 448let ret += [ s: msgpack_dump_string ({ '_VAL' : split ( k , "\n" )}), 449 \': ' , 450 \msgpack#string ( v ), 451 \', ' ] 452unlet v 453endfor 454if ! empty ( a: v ) 455call remove ( ret , -1 ) 456endif 457else 458for [ k , v ] in sort ( copy ( a: v . _VAL )) 459let ret += [ msgpack#string ( k ), 460 \': ' , 461 \msgpack#string ( v ), 462 \', ' ] 463unlet k 464unlet v 465endfor 466if ! empty ( a: v . _VAL ) 467call remove ( ret , -1 ) 468endif 469endif 470let ret += [ '}' ] 471return join ( ret , '' ) 472endfunction 473 474"" 475" Dump extension value. 476function s: msgpack_dump_ext ( v ) abort 477return printf ( '+(%i)%s' , a: v . _VAL [ 0 ], 478 \s: msgpack_dump_string ({ '_VAL' : a: v . _VAL [ 1 ]})) 479endfunction 480 481"" 482" Convert msgpack object to a string, like string() function does. Result of the 483" conversion may be passed to msgpack#eval(). 484function msgpack#string ( v ) abort 485if type ( a: v ) == type ({}) 486let type = msgpack#special_type ( a: v ) 487if type is 0 488let type = 'map' 489endif 490else 491let type = get ( s: MSGPACK_STANDARD_TYPES , type ( a: v ), 0 ) 492if type is 0 493throw printf ( 'msgpack:invtype: Unable to convert value %s' , string ( a: v )) 494endif 495endif 496return s: msgpack_dump_ { type }( a: v ) 497endfunction 498 499"" 500" Copy msgpack object like deepcopy() does, but leave types intact 501function msgpack#deepcopy ( obj ) abort 502if type ( a: obj ) == type ([]) 503return map ( copy ( a: obj ), 'msgpack#deepcopy(v:val)' ) 504elseif type ( a: obj ) == type ({}) 505let special_type = msgpack#special_type ( a: obj ) 506if special_type is 0 507return map ( copy ( a: obj ), 'msgpack#deepcopy(v:val)' ) 508else 509return { 510 \'_TYPE' : v: msgpack_types [ special_type ], 511 \'_VAL' : msgpack#deepcopy ( a: obj . _VAL ) 512 \} 513endif 514else 515return copy ( a: obj ) 516endif 517endfunction 518 519"" 520" Convert an escaped character to needed value 521function s: msgpack_eval_str_sub ( ch ) abort 522if a: ch is# 'n' 523return '", "' 524elseif a: ch is# '0' 525return '\n' 526else 527return '\' . a: ch 528endif 529endfunction 530 531let s: MSGPACK_SPECIAL_OBJECTS = { 532 \'NIL' : '{''_TYPE'': v:msgpack_types.nil, ''_VAL'': 0}' , 533 \'TRUE' : '{''_TYPE'': v:msgpack_types.boolean, ''_VAL'': 1}' , 534 \'FALSE' : '{''_TYPE'': v:msgpack_types.boolean, ''_VAL'': 0}' , 535 \'nan' : '(-(1.0/0.0-1.0/0.0))' , 536 \'inf' : '(1.0/0.0)' , 537\} 538 539"" 540" Convert msgpack object dumped by msgpack#string() to a Vimscript object 541" suitable for msgpackdump(). 542" 543" @param[in] s String to evaluate. 544" @param[in] special_objs Additional special objects, in the same format as 545" s:MSGPACK_SPECIAL_OBJECTS. 546" 547" @return Any value that msgpackparse() may return. 548function msgpack#eval ( s , special_objs ) abort 549let s = a: s 550let expr = [] 551let context = [] 552while ! empty ( s ) 553let s = substitute ( s , '^\s*' , '' , '' ) 554if s [ 0 ] =~# '\v^\h$' 555let name = matchstr ( s , '\v\C^\w+' ) 556if has_key ( s: MSGPACK_SPECIAL_OBJECTS , name ) 557call add ( expr , s: MSGPACK_SPECIAL_OBJECTS [ name ]) 558elseif has_key ( a: special_objs , name ) 559call add ( expr , a: special_objs [ name ]) 560else 561throw 'name-unknown:Unknown name ' . name . ': ' . s 562endif 563let s = s [ len ( name ):] 564elseif ( s [ 0 ] is# '-' && s [ 1 ] =~# '\v^\d$' ) || s [ 0 ] =~# '\v^\d$' 565let sign = 1 566if s [ 0 ] is# '-' 567let s = s [ 1 :] 568let sign = -1 569endif 570if s [ 0 : 1 ] is# '0x' 571" See comment in msgpack#int_dict_to_str(). 572let s = s [ 2 :] 573let hexnum = matchstr ( s , '\v\C^\x+' ) 574if empty ( hexnum ) 575throw '0x-empty:Must have number after 0x: ' . s 576elseif len ( hexnum ) > 16 577throw '0x-long:Must have at most 16 hex digits: ' . s 578endif 579let s = s [ len ( hexnum ):] 580let hexnum = repeat ( '0' , 16 - len ( hexnum )) . hexnum 581let g1 = str2nr ( hexnum [ 0 ], 16 ) 582let g2 = str2nr ( hexnum [ 1 : 7 ], 16 ) 583let g3 = str2nr ( hexnum [ 8 ], 16 ) 584let g4 = str2nr ( hexnum [ 9 : 15 ], 16 ) 585let v1 = s: shift ( g1 , -2 ) 586let v2 = or ( or ( s: shift ( s: mask1 ( g1 , 2 ), 29 ), s: shift ( g2 , 1 )), 587 \s: mask1 ( s: shift ( g3 , -3 ), 1 )) 588let v3 = or ( s: shift ( s: mask1 ( g3 , 3 ), 28 ), g4 ) 589call add ( expr , printf ( '{''_TYPE'': v:msgpack_types.integer, ' . 590 \'''_VAL'': [%i, %u, %u, %u]}' , 591 \sign , v1 , v2 , v3 )) 592else 593let num = matchstr ( s , '\v\C^\d+' ) 594let s = s [ len ( num ):] 595if sign == -1 596call add ( expr , '-' ) 597endif 598call add ( expr , num ) 599if s [ 0 ] is# '.' 600let dec = matchstr ( s , '\v\C^\.\d+%(e[+-]?\d+)?' ) 601if empty ( dec ) 602throw '0.-nodigits:Decimal dot must be followed by digit(s): ' . s 603endif 604let s = s [ len ( dec ):] 605call add ( expr , dec ) 606endif 607endif 608elseif s =~# '\v^\-%(inf|nan)' 609call add ( expr , '-' ) 610call add ( expr , s: MSGPACK_SPECIAL_OBJECTS [ s [ 1 : 3 ]]) 611let s = s [ 4 :] 612elseif stridx ( '="+' , s [ 0 ]) != -1 613let match = matchlist ( s , '\v\C^(\=|\+\((\-?\d+)\)|)(\"%(\\.|[^\\"]+)*\")' ) 614if empty ( match ) 615throw '"-invalid:Invalid string: ' . s 616endif 617call add ( expr , '{''_TYPE'': v:msgpack_types.' ) 618if empty ( match [ 1 ]) || match [ 1 ] is# '=' 619call add ( expr , 'string' ) 620else 621call add ( expr , 'ext' ) 622endif 623call add ( expr , ', ''_VAL'': [' ) 624if match [ 1 ][ 0 ] is# '+' 625call add ( expr , match [ 2 ] . ', [' ) 626endif 627call add ( expr , substitute ( match [ 3 ], '\v\C\\(.)' , 628 \'\=s:msgpack_eval_str_sub(submatch(1))' , 'g' )) 629if match [ 1 ][ 0 ] is# '+' 630call add ( expr , ']' ) 631endif 632call add ( expr , ']}' ) 633let s = s [ len ( match [ 0 ]):] 634elseif s [ 0 ] is# '{' 635call add ( context , 'map' ) 636call add ( expr , '{''_TYPE'': v:msgpack_types.map, ''_VAL'': [' ) 637call add ( expr , '[' ) 638let s = s [ 1 :] 639elseif s [ 0 ] is# '[' 640call add ( context , 'array' ) 641call add ( expr , '[' ) 642let s = s [ 1 :] 643elseif s [ 0 ] is# ':' 644call add ( expr , ',' ) 645let s = s [ 1 :] 646elseif s [ 0 ] is# ',' 647if context [ -1 ] is# 'array' 648call add ( expr , ',' ) 649else 650call add ( expr , '], [' ) 651endif 652let s = s [ 1 :] 653elseif s [ 0 ] is# ']' 654call remove ( context , -1 ) 655call add ( expr , ']' ) 656let s = s [ 1 :] 657elseif s [ 0 ] is# '}' 658call remove ( context , -1 ) 659if expr [ -1 ] is# "\x5B" 660call remove ( expr , -1 ) 661else 662call add ( expr , ']' ) 663endif 664call add ( expr , ']}' ) 665let s = s [ 1 :] 666elseif s [ 0 ] is# '''' 667let char = matchstr ( s , '\v\C^\''\zs%(\\\d+|.)\ze\''' ) 668if empty ( char ) 669throw 'char-invalid:Invalid integer character literal format: ' . s 670endif 671if char [ 0 ] is# '\' 672call add ( expr , + char [ 1 :]) 673else 674call add ( expr , char2nr ( char )) 675endif 676let s = s [ len ( char ) + 2 :] 677else 678throw 'unknown:Invalid non-space character: ' . s 679endif 680endwhile 681if empty ( expr ) 682throw 'empty:Parsed string is empty' 683endif 684return eval ( join ( expr , '' )) 685endfunction 686 687"" 688" Check whether two msgpack values are equal 689function msgpack#equal ( a , b ) 690let atype = msgpack#type ( a: a ) 691let btype = msgpack#type ( a: b ) 692if atype isnot# btype 693return 0 694endif 695let aspecial = msgpack#special_type ( a: a ) 696let bspecial = msgpack#special_type ( a: b ) 697if aspecial is# bspecial 698if aspecial is# 0 699if type ( a: a ) == type ({}) 700if len ( a: a ) != len ( a: b ) 701return 0 702endif 703if ! empty ( filter ( keys ( a: a ), '!has_key(a:b, v:val)' )) 704return 0 705endif 706for [ k , v ] in items ( a: a ) 707if ! msgpack#equal ( v , a: b [ k ]) 708return 0 709endif 710unlet v 711endfor 712return 1 713elseif type ( a: a ) == type ([]) 714if len ( a: a ) != len ( a: b ) 715return 0 716endif 717let i = 0 718for asubval in a: a 719if ! msgpack#equal ( asubval , a: b [ i ]) 720return 0 721endif 722let i += 1 723unlet asubval 724endfor 725return 1 726elseif type ( a: a ) == type ( 0.0 ) 727return ( a: a == a: a ? a: a == a: b : string ( a: a ) ==# string ( a: b )) 728else 729return a: a ==# a: b 730endif 731elseif aspecial is# 'map' || aspecial is# 'array' 732if len ( a: a . _VAL ) != len ( a: b . _VAL ) 733return 0 734endif 735let alist = aspecial is# 'map' ? sort ( copy ( a: a . _VAL )) : a: a . _VAL 736let blist = bspecial is# 'map' ? sort ( copy ( a: b . _VAL )) : a: b . _VAL 737let i = 0 738for asubval in alist 739let bsubval = blist [ i ] 740if aspecial is# 'map' 741if !( msgpack#equal ( asubval [ 0 ], bsubval [ 0 ]) 742 \&& msgpack#equal(asubval[1], bsubval[1])) 743return 0 744endif 745else 746if ! msgpack#equal ( asubval , bsubval ) 747return 0 748endif 749endif 750let i += 1 751unlet asubval 752unlet bsubval 753endfor 754return 1 755elseif aspecial is# 'nil' 756return 1 757elseif aspecial is# 'float' 758return ( a: a . _VAL == a: a . _VAL 759 \? (a:a._VAL == a:b._VAL) 760 \: (string(a:a._VAL) ==# string(a:b._VAL))) 761else 762return a: a . _VAL ==# a: b . _VAL 763endif 764else 765if atype is# 'array' 766let a = aspecial is 0 ? a: a : a: a . _VAL 767let b = bspecial is 0 ? a: b : a: b . _VAL 768return msgpack#equal ( a , b ) 769elseif atype is# 'string' 770let a = ( aspecial is 0 ? split ( a: a , "\n" , 1 ) : a: a . _VAL ) 771let b = ( bspecial is 0 ? split ( a: b , "\n" , 1 ) : a: b . _VAL ) 772return a ==# b 773elseif atype is# 'map' 774if aspecial is 0 775let akeys = copy ( a: a ) 776if len ( a: b . _VAL ) != len ( akeys ) 777return 0 778endif 779for [ k , v ] in a: b . _VAL 780if msgpack#type ( k ) isnot# 'string' 781" Non-special mapping cannot have non-string keys 782return 0 783endif 784if type ( k ) == type ({}) 785if ( empty ( k . _VAL ) 786 \|| k . _VAL ==# [ "" ] 787 \|| ! empty ( filter ( copy ( k . _VAL ), 'stridx(v:val, "\n") != -1' ))) 788" Non-special mapping cannot have zero byte in key or an empty key 789return 0 790endif 791let kstr = join ( k . _VAL , "\n" ) 792else 793let kstr = k 794endif 795if ! has_key ( akeys , kstr ) 796" Protects from both missing and duplicate keys 797return 0 798endif 799if ! msgpack#equal ( akeys [ kstr ], v ) 800return 0 801endif 802call remove ( akeys , kstr ) 803unlet k 804unlet v 805endfor 806return 1 807else 808return msgpack#equal ( a: b , a: a ) 809endif 810elseif atype is# 'float' 811let a = aspecial is 0 ? a: a : a: a . _VAL 812let b = bspecial is 0 ? a: b : a: b . _VAL 813return ( a == a ? a == b : string ( a ) ==# string ( b )) 814elseif atype is# 'integer' 815if aspecial is 0 816let sign = a: a >= 0 ? 1 : -1 817let a = sign * a: a 818let v1 = s: mask1 ( s: shift ( a , -62 ), 2 ) 819let v2 = s: mask1 ( s: shift ( a , -31 ), 31 ) 820let v3 = s: mask1 ( a , 31 ) 821return [ sign , v1 , v2 , v3 ] == a: b . _VAL 822else 823return msgpack#equal ( a: b , a: a ) 824endif 825else 826throw printf ( 'internal-invalid-type: %s == %s, but special %s /= %s' , 827 \atype , btype , aspecial , bspecial ) 828endif 829endif 830endfunction