Permalink
Mar 6, 2021
Mar 19, 2018
Mar 19, 2018
Apr 5, 2021
Mar 6, 2021
Mar 19, 2018
Apr 25, 2020
Mar 6, 2021
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 6, 2021
Mar 19, 2018
Mar 6, 2021
Mar 6, 2021
Mar 30, 2021
Jan 14, 2015
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Jan 14, 2015
Mar 19, 2018
Jan 14, 2015
Jan 14, 2015
Jan 14, 2015
Mar 19, 2018
Jan 14, 2015
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Jan 14, 2015
Mar 19, 2018
Jan 14, 2015
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Dec 18, 2019
Dec 18, 2019
Mar 19, 2018
Jan 14, 2015
Jan 14, 2015
Mar 19, 2018
Mar 19, 2018
Jan 14, 2015
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 6, 2021
Mar 19, 2018
Mar 16, 2021
Mar 16, 2021
Mar 19, 2018
Mar 27, 2019
Mar 6, 2021
Mar 19, 2018
Mar 6, 2021
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 6, 2021
Mar 6, 2021
Mar 6, 2021
Mar 19, 2018
Apr 2, 2019
Apr 2, 2019
Mar 19, 2018
Aug 31, 2017
Mar 19, 2018
Mar 6, 2021
Mar 19, 2018
Mar 6, 2021
Aug 31, 2017
Apr 16, 2019
Mar 19, 2018
Apr 16, 2019
Mar 6, 2021
Mar 19, 2018
Mar 6, 2021
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Nov 12, 2018
Nov 22, 2020
Nov 12, 2018
Mar 19, 2018
Nov 22, 2020
Mar 6, 2021
Mar 6, 2021
Mar 6, 2021
Mar 6, 2021
Mar 6, 2021
Mar 6, 2021
Mar 6, 2021
Sep 27, 2020
Sep 27, 2020
Mar 6, 2021
Mar 6, 2021
Mar 6, 2021
Mar 19, 2018
Mar 6, 2021
Mar 19, 2018
Mar 6, 2021
Mar 19, 2018
Aug 31, 2017
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Jan 6, 2016
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 6, 2021
Jun 11, 2020
Mar 19, 2018
Mar 19, 2018
Apr 5, 2021
Mar 6, 2021
Mar 6, 2021
Mar 19, 2018
Mar 19, 2018
Aug 31, 2017
May 24, 2019
Mar 19, 2018
Mar 6, 2021
Mar 6, 2021
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 6, 2021
Mar 19, 2018
Mar 6, 2021
Mar 6, 2021
Mar 6, 2021
Mar 19, 2018
Feb 27, 2020
Feb 27, 2020
Feb 27, 2020
Aug 31, 2017
Mar 19, 2018
Mar 6, 2021
Mar 19, 2018
Mar 6, 2021
Mar 6, 2021
Mar 6, 2021
Mar 6, 2021
Mar 6, 2021
Mar 6, 2021
Jan 26, 2020
Mar 6, 2021
Nov 15, 2019
Jul 28, 2018
Oct 27, 2019
Nov 2, 2018
Oct 27, 2019
Nov 2, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 6, 2021
Nov 12, 2018
Mar 6, 2021
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Dec 12, 2020
Jun 29, 2017
Oct 13, 2019
Jun 29, 2017
Dec 12, 2020
Jul 10, 2017
Jul 10, 2017
Mar 19, 2018
Mar 19, 2018
Jun 20, 2020
Nov 30, 2020
Oct 13, 2019
Jan 18, 2021
Jan 18, 2021
Newer
100644
2805 lines (2591 sloc)
85.1 KB
8
9
$B.has_surrogate = function(s){
10
// Check if there are "surrogate pairs" characters in string s
11
for(var i = 0; i < s.length; i++){
12
code = s.charCodeAt(i)
13
if(code >= 0xD800 && code <= 0xDBFF){
14
return true
15
}
16
}
17
return false
18
}
19
32
if($.start === null || $.start === _b_.None){$.start = 0}
33
else if($.start < 0){
34
$.start += $.self.length
35
$.start = Math.max(0, $.start)
36
}
37
if($.end === null || $.end === _b_.None){$.end = $.self.length}
38
else if($.end < 0){
39
$.end += $.self.length
40
$.end = Math.max(0, $.end)
41
}
44
throw _b_.TypeError.$factory("slice indices must be integers " +
45
"or None or have an __index__ method")
46
}
56
throw _b_.TypeError.$factory((prefix || '') +
57
"must be str, not " + $B.class_name(obj))
61
function to_chars(s){
62
// Transform Javascript string s into a list of Python characters
63
// (2 JS chars if surrogate, 1 otherwise)
64
var chars = []
65
for(var i = 0, len = s.length; i < len; i++){
66
var code = s.charCodeAt(i)
67
if(code >= 0xD800 && code <= 0xDBFF){
68
chars.push(s.substr(i, 2))
69
i++
70
}else{
71
chars.push(s.charAt(i))
72
}
73
}
74
return chars
75
}
76
77
function to_codepoints(s){
78
// Transform Javascript string s into a list of codepoints
79
var cps = []
80
for(var i = 0, len = s.length; i < len; i++){
81
var code = s.charCodeAt(i)
82
if(code >= 0xD800 && code <= 0xDBFF){
83
var v = 0x10000
84
v += (code & 0x03FF) << 10
85
v += (s.charCodeAt(i + 1) & 0x03FF)
86
cps.push(v)
87
i++
88
}else{
89
cps.push(code)
90
}
91
}
92
return cps
93
}
94
107
throw _b_.TypeError.$factory("'in <string>' requires " +
108
"string as left operand, not " + item.__class__)
109
}
110
if(typeof item == "string"){
111
var nbcar = item.length
112
}else{
113
var nbcar = _b_.len(item)
114
}
115
if(nbcar == 0){
116
// a string contains the empty string
117
return true
118
}
119
if(self.length == 0){
120
return nbcar == 0
121
}
134
// __dir__must be assigned explicitely because attribute resolution for
135
// builtin classes doesn't use __mro__
147
if(fmt.type && fmt.type != "s"){
148
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
156
if(fmt.sign !== undefined){
157
throw _b_.ValueError.$factory(
158
"Sign not allowed in string format specifier")
172
if(arg < 0){
173
pos += self.length
174
}
175
if(pos >= 0 && pos < chars.length){
176
return chars[pos]
177
}
181
var s = _b_.slice.$conv_for_seq(arg, self.length),
182
start = s.start,
183
stop = s.stop,
184
step = s.step
185
var res = "",
188
if(stop <= start){
189
return ""
190
}
191
for(var i = start; i < stop; i += step){
192
res += chars[i]
193
}
195
if(stop >= start){
196
return ''
197
}
198
for(var i = start; i > stop; i += step){
199
res += chars[i]
200
}
261
// found at
262
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
263
return [...self].length
300
// left adjusted
301
return s + get_char_array(padding - s.length, flags.pad_char)
302
}
303
}
304
312
if(val.__class__ === $B.long_int){
313
s = $B.long_int.to_base(val, 10)
314
}else{
315
s = val.toString()
317
if(s[0] === "-"){
318
return "-" + get_char_array(precision - s.length + 1, "0") + s.slice(1)
329
if(val === Infinity){
330
val = "inf"
331
}else if(val === -Infinity){
332
val = "-inf"
333
}else{
334
val = "nan"
356
var str_format = function(val, flags) {
357
// string format supports left and right padding
358
flags.pad_char = " " // even if 0 padding is defined, don't use it
364
if(val.__class__ === $B.long_int){
365
val = $B.long_int.to_base(val, 10)
366
}else{
367
val = parseInt(val)
385
var repr_format = function(val, flags) {
386
flags.pad_char = " " // even if 0 padding is defined, don't use it
390
var ascii_format = function(val, flags) {
391
flags.pad_char = " " // even if 0 padding is defined, don't use it
405
flags.precision = parseInt(flags.precision, 10)
406
validate_precision(flags.precision)
407
}
408
return parseFloat(val)
409
}
412
var trailing_zeros = /(.*?)(0+)([eE].*)/,
413
leading_zeros = /\.(0*)/,
414
trailing_dot = /\.$/
416
var validate_precision = function(precision) {
417
// force precision to limits of javascript
422
var floating_point_format = function(val, upper, flags){
423
val = _float_helper(val, flags),
424
v = val.toString(),
425
v_len = v.length,
426
dot_idx = v.indexOf('.')
427
if(dot_idx < 0){dot_idx = v_len}
428
if(val < 1 && val > -1){
429
var zeros = leading_zeros.exec(v),
430
numzeros
431
if(zeros){
436
if(numzeros >= 4){
437
val = format_sign(val, flags) + format_float_precision(val, upper,
438
flags, _floating_g_exp_helper)
439
if(!flags.alternate){
452
return format_padding(format_sign(val, flags) +
453
format_float_precision(val, upper, flags,
454
function(val, precision) {
456
numzeros)
457
}),
458
flags
459
)
460
}
461
462
if(dot_idx > flags.precision){
463
val = format_sign(val, flags) + format_float_precision(val, upper,
464
flags, _floating_g_exp_helper)
465
if(! flags.alternate){
477
return format_padding(format_sign(val, flags) +
478
format_float_precision(val, upper, flags,
479
function(val, precision) {
480
if(!flags.decimal_point){
482
}else if (precision > v_len){
483
if(! flags.alternate){
484
precision = v_len
485
}
487
if(precision < dot_idx){
488
precision = dot_idx
489
}
490
return val.toFixed(precision - dot_idx)
491
}),
492
flags
493
)
496
var _floating_g_exp_helper = function(val, precision, flags, upper){
497
if(precision){--precision}
500
var e_idx = val.lastIndexOf("e")
501
if(e_idx > val.length - 4){
502
val = val.substring(0, e_idx + 2) + "0" + val.substring(e_idx + 2)
505
return val
506
}
507
508
// fF
509
var floating_point_decimal_format = function(val, upper, flags) {
510
val = _float_helper(val, flags)
511
return format_padding(format_sign(val, flags) +
512
format_float_precision(val, upper, flags,
513
function(val, precision, flags) {
514
val = val.toFixed(precision)
515
if(precision === 0 && flags.alternate){
516
val += '.'
517
}
518
return val
519
}),
520
flags
521
)
522
}
523
524
var _floating_exp_helper = function(val, precision, flags, upper) {
525
val = val.toExponential(precision)
526
// pad exponent to two digits
539
return format_padding(format_sign(val, flags) +
540
format_float_precision(val, upper, flags, _floating_exp_helper), flags)
566
if(flags.alternate){
567
if(ret.charAt(0) === "-"){
568
if(upper){ret = "-0X" + ret.slice(1)}
569
else{ret = "-0x" + ret.slice(1)}
570
}else{
571
if(upper){ret = "0X" + ret}
572
else{ret = "0x" + ret}
582
if(val.__class__ === $B.long_int){
583
ret = $B.long_int.to_base(8)
584
}else{
585
ret = parseInt(val)
586
ret = ret.toString(8)
602
if(flags.alternate){
603
if(ret.charAt(0) === "-"){ret = "-0o" + ret.slice(1)}
604
else{ret = "0o" + ret}
609
function series_of_bytes(val, flags){
610
if(val.__class__ && val.__class__.$buffer_protocol){
611
var it = _b_.iter(val),
612
ints = []
613
while(true){
614
try{
615
ints.push(_b_.next(it))
616
}catch(err){
617
if(err.__class__ === _b_.StopIteration){
618
var b = _b_.bytes.$factory(ints)
619
return format_padding(_b_.bytes.decode(b, "ascii"), flags)
620
}
621
throw err
622
}
623
}
624
}else{
625
try{
626
bytes_obj = $B.$getattr(val, "__bytes__")
627
return format_padding(_b_.bytes.decode(bytes_obj), flags)
628
}catch(err){
629
if(err.__class__ === _b_.AttributeError){
630
throw _b_.TypeError.$factory("%b does not accept '" +
631
$B.class_name(val) + "'")
632
}
633
throw err
634
}
635
}
636
}
637
642
val = val.source[0]
643
}else{
644
try{
645
val = _b_.int.$factory(val) // yes, floats are valid (they are cast to int)
646
}catch (err){
647
throw _b_.TypeError.$factory("%c requires int or char")
648
}
653
var num_flag = function(c, flags){
654
if(c === "0" && ! flags.padding && ! flags.decimal_point && ! flags.left){
655
flags.pad_char = "0"
661
flags.precision = (flags.precision || "") + c
662
}
663
}
664
665
var decimal_point_flag = function(val, flags) {
667
// can only have one decimal point
668
throw new UnsupportedChar()
669
}
670
flags.decimal_point = true
671
}
672
673
var neg_flag = function(val, flags){
674
flags.pad_char = " " // overrides '0' flag
692
"s": str_format,
693
"d": num_format,
694
"i": num_format,
695
"u": num_format,
696
"o": octal_format,
697
"r": repr_format,
698
"a": ascii_format,
699
"g": function(val, flags){
700
return floating_point_format(val, false, flags)
701
},
702
"G": function(val, flags){return floating_point_format(val, true, flags)},
703
"f": function(val, flags){
704
return floating_point_decimal_format(val, false, flags)
705
},
706
"F": function(val, flags){
707
return floating_point_decimal_format(val, true, flags)
708
},
709
"e": function(val, flags){
710
return floating_point_exponential_format(val, false, flags)
711
},
712
"E": function(val, flags){
713
return floating_point_exponential_format(val, true, flags)
714
},
715
"x": function(val, flags){return signed_hex_format(val, false, flags)},
716
"X": function(val, flags){return signed_hex_format(val, true, flags)},
717
"c": single_char_format,
718
"0": function(val, flags){return num_flag("0", flags)},
719
"1": function(val, flags){return num_flag("1", flags)},
720
"2": function(val, flags){return num_flag("2", flags)},
721
"3": function(val, flags){return num_flag("3", flags)},
722
"4": function(val, flags){return num_flag("4", flags)},
723
"5": function(val, flags){return num_flag("5", flags)},
724
"6": function(val, flags){return num_flag("6", flags)},
725
"7": function(val, flags){return num_flag("7", flags)},
726
"8": function(val, flags){return num_flag("8", flags)},
727
"9": function(val, flags){return num_flag("9", flags)},
728
"-": neg_flag,
729
" ": space_flag,
730
"+": sign_flag,
731
".": decimal_point_flag,
732
"#": alternate_flag
733
}
734
735
// exception thrown when an unsupported char is encountered in legacy format
781
if(self === undefined){
782
throw _b_.TypeError.$factory(
783
"not enough arguments for format string")
810
throw _b_.ValueError.$factory(
811
"unsupported format character '" + invalid_char +
812
"' (0x" + invalid_char.charCodeAt(0).toString(16) +
813
") at index " + newpos)
814
}else if(err.name === "NotANumber"){
815
var try_char = s[newpos],
816
cls = self.__class__
817
if(!cls){
818
if(typeof(self) === "string"){
819
cls = "str"
820
}else{
826
throw _b_.TypeError.$factory("%" + try_char +
827
" format: a number is required, not " + cls)
828
}else{
860
}while(pos < length)
861
862
if(argpos !== null){
863
if(args.length > argpos){
864
throw _b_.TypeError.$factory(
865
"not enough arguments for format string")
866
}else if(args.length < argpos){
867
throw _b_.TypeError.$factory(
868
"not all arguments converted during string formatting")
870
}else if(nbph == 0){
871
throw _b_.TypeError.$factory(
872
"not all arguments converted during string formatting")
880
var $ = $B.args("__mul__", 2, {self: null, other: null},
881
["self", "other"], arguments, {}, null, null)
892
function __newobj__(){
893
// __newobj__ is called with a generator as only argument
894
var $ = $B.args('__newobj__', 0, {}, [], arguments, {}, 'args', null),
895
args = $.args
896
var res = args[1]
897
res.__class__ = args[0]
898
return res
899
}
900
901
str.__reduce_ex__ = function(self){
902
return $B.fast_tuple([
903
__newobj__,
904
$B.fast_tuple([self.__class__ || _b_.str, self]),
905
_b_.None,
906
_b_.None])
907
}
908
911
var t = {
912
8: "\\x08",
913
9: "\\t",
914
10: "\\n",
915
11: "\\x0b",
916
12: "\\x0c",
917
13: "\\r",
918
92: "\\\\"
919
}
920
var repl = '',
921
chars = to_chars(self)
922
for(var i = 0; i < chars.length; i++){
923
var cp = _b_.ord(chars[i])
924
if(t[cp] !== undefined){
925
repl += t[cp]
926
}else if($B.is_unicode_cn(cp)){
945
if(res.search('"') == -1 && res.search("'") == -1){
946
return "'" + res + "'"
947
}else if(self.search('"') == -1){
948
return '"' + res + '"'
949
}
950
var qesc = new RegExp("'", "g") // to escape single quote
951
res = "'" + res.replace(qesc, "\\'") + "'"
955
str.__setattr__ = function(self, attr, value){
956
if(typeof self === "string"){
957
if(str.hasOwnProperty(attr)){
959
attr + "' is read-only")
960
}else{
961
throw _b_.AttributeError.$factory(
962
"'str' object has no attribute '" + attr + "'")
963
}
964
}
965
// str subclass : use __dict__
966
_b_.dict.$setitem(self.__dict__, attr, value)
967
return $N
968
}
969
970
str.__setitem__ = function(self, attr, value){
971
throw _b_.TypeError.$factory(
972
"'str' object does not support item assignment")
975
var combining = []
976
for(var cp = 0x300; cp <= 0x36F; cp++){
977
combining.push(String.fromCharCode(cp))
978
}
982
var repl = '',
983
chars = to_chars(self)
984
if(chars.length == self.length){
985
return self.replace(combining_re, "\u200B$1")
986
}
987
for(var i = 0; i < chars.length; i++){
988
var cp = _b_.ord(chars[i])
989
if(cp >= 0x300 && cp <= 0x36F){
990
repl += "\u200B" + chars[i]
991
}else{
992
repl += chars[i]
993
}
994
}
995
return repl
1005
$comp_func += "" // source code
1006
var $comps = {">": "gt", ">=": "ge", "<": "lt", "<=": "le"}
1020
str.capitalize = function(self){
1021
var $ = $B.args("capitalize", 1, {self}, ["self"],
1022
arguments, {}, null, null)
1026
return self.charAt(0).toUpperCase() + self.substr(1)
1027
}
1028
1029
str.casefold = function(self){
1030
var $ = $B.args("casefold", 1, {self}, ["self"],
1031
arguments, {}, null, null),
1032
res = "",
1033
char,
1034
cf,
1035
chars = to_chars($.self)
1036
1037
for(var i = 0, len = chars.length; i < len; i++){
1038
char = chars[i]
1039
cf = $B.unicode_casefold[char]
1040
if(cf){
1041
cf.forEach(function(cp){
1042
res += String.fromCharCode(cp)
1043
})
1044
}else{
1051
str.center = function(){
1052
var $ = $B.args("center", 3, {self: null, width: null, fillchar: null},
1053
["self", "width", "fillchar"],
1054
arguments, {fillchar:" "}, null, null),
1055
self = $.self
1059
var pad = parseInt(($.width - self.length) / 2),
1060
res = $.fillchar.repeat(pad)
1069
var $ = $B.args("count", 4, {self:null, sub:null, start:null, stop:null},
1070
["self", "sub", "start", "stop"], arguments, {start:null, stop:null},
1072
if(!(typeof $.sub == "string")){
1073
throw _b_.TypeError.$factory("Can't convert '" + $B.class_name($.sub) +
1074
"' object to str implicitly")
1075
}
1079
if($.stop !== null){
1080
_slice = _b_.slice.$factory($.start, $.stop)
1081
}else{
1082
_slice = _b_.slice.$factory($.start, $.self.length)
1083
}
1091
if($.start == $.self.length){
1092
return 1
1093
}else if(substr.length == 0){
1094
return 0
1095
}
1098
var n = 0,
1099
pos = 0
1100
while(pos < substr.length){
1101
pos = substr.indexOf($.sub, pos)
1112
str.encode = function(){
1113
var $ = $B.args("encode", 3, {self: null, encoding: null, errors: null},
1114
["self", "encoding", "errors"], arguments,
1115
{encoding: "utf-8", errors: "strict"}, null, null)
1116
if($.encoding == "rot13" || $.encoding == "rot_13"){
1119
for(var i = 0, len = $.self.length; i < len ; i++){
1120
var char = $.self.charAt(i)
1121
if(("a" <= char && char <= "m") || ("A" <= char && char <= "M")){
1122
res += String.fromCharCode(String.charCodeAt(char) + 13)
1123
}else if(("m" < char && char <= "z") ||
1124
("M" < char && char <= "Z")){
1125
res += String.fromCharCode(String.charCodeAt(char) - 13)
1134
// Return True if the string ends with the specified suffix, otherwise
1135
// return False. suffix can also be a tuple of suffixes to look for.
1136
// With optional start, test beginning at that position. With optional
1140
["self", "suffix", "start", "end"],
1141
arguments, {start: 0, end: null}, null, null)
1154
if(! _b_.isinstance(suffix, str)){
1155
throw _b_.TypeError.$factory(
1156
"endswith first arg must be str or a tuple of str, not int")
1157
}
1167
var $ = $B.args("expandtabs", 2, {self: null, tabsize: null},
1168
["self", "tabsize"], arguments, {tabsize: 8}, null, null)
1169
var s = $B.$GetInt($.tabsize),
1170
col = 0,
1171
pos = 0,
1172
res = "",
1173
chars = to_chars(self)
1174
if(s == 1){
1175
return self.replace(/\t/g," ")
1176
}
1177
while(pos < chars.length){
1178
var car = chars[pos]
1188
res += car
1189
col = 0
1190
break
1191
default:
1192
res += car
1193
col++
1194
break
1195
}
1196
pos++
1197
}
1198
return res
1202
// Return the lowest index in the string where substring sub is found,
1203
// such that sub is contained in the slice s[start:end]. Optional
1204
// arguments start and end are interpreted as in slice notation.
1207
{self: null, sub: null, start: null, end: null},
1208
["self", "sub", "start", "end"],
1209
arguments, {start: 0, end: null}, null, null)
1215
throw _b_.TypeError.$factory("slice indices must be " +
1216
"integers or None or have an __index__ method")}
1217
// Can't use string.substring(start, end) because if end < start,
1218
// Javascript transforms it into substring(end, start)...
1219
var s = ""
1220
for(var i = $.start; i < $.end; i++){
1221
s += $.self.charAt(i)
1222
}
1224
var len = str.__len__($.self)
1225
1226
if($.sub.length == 0 && $.start == len){
1227
return len
1228
}
1229
if(s.length + $.sub.length == 0){
1230
return -1
1231
}
1233
var last_search = s.length - $.sub.length
1234
for(var i = 0; i <= last_search; i++){
1235
if(s.substr(i, $.sub.length) == $.sub){
1236
return $.start + str.__len__(s.substr(0, i))
1237
}
1248
// a.x[z]!r:...
1249
// the object has attributes :
1250
// - name : "a"
1251
// - name_ext : [".x", "[z]"]
1252
// - conv : r
1253
// - spec : rest of string after :
1261
// No : in the string : it only contains a name
1262
name = fmt_string
1263
}else{
1264
// name is before the first ":"
1265
// spec (the format specification) is after
1266
name = elts[0]
1270
var elts = name.split("!")
1271
if(elts.length > 1){
1272
name = elts[0]
1273
conv = elts[1] // conversion flag
1277
// "name' may be a subscription or attribute
1278
// Put these "extensions" in the list "name_ext"
1279
function name_repl(match){
1280
name_ext.push(match)
1282
}
1283
var name_ext_re = /\.[_a-zA-Z][_a-zA-Z0-9]*|\[[_a-zA-Z][_a-zA-Z0-9]*\]|\[[0-9]+\]/g
1284
name = name.replace(name_ext_re, name_repl)
1285
}
1292
// Parse self to detect formatting instructions
1293
// Create a list "parts" made of sections of the string :
1294
// - elements of even rank are literal text
1295
// - elements of odd rank are "format objects", built from the
1296
// format strings in self (of the form {...})
1307
text += "{"
1308
pos += 2
1309
}else if(car == "}" && self.charAt(pos + 1) == "}"){
1316
// Store current literal text
1317
parts.push(text)
1318
1319
// Search the end of the format string, ie the } closing the
1320
// opening {. Since the string can contain other pairs {} for
1321
// nested formatting, an integer nb is incremented for each { and
1322
// decremented for each } ; the end of the format string is
1323
// reached when nb == 0
1324
var end = pos + 1,
1325
nb = 1
1326
while(end < _len){
1327
if(self.charAt(end) == "{"){nb++; end++}
1328
else if(self.charAt(end) == "}"){
1329
nb--; end++
1330
if(nb == 0){
1335
var fmt_obj = $B.parse_format(fmt_string)
1336
fmt_obj.raw_name = fmt_obj.name
1337
fmt_obj.raw_spec = fmt_obj.spec
1371
}else{
1372
text += car
1373
pos++
1374
}
1375
}
1376
if(text){
1377
parts.push(text)
1383
// Special management of keyword arguments if str.format is called by
1384
// str.format_map(mapping) : the argument "mapping" might not be a
1385
// dictionary
1386
var last_arg = $B.last(arguments)
1387
if(last_arg.$nat == "mapping"){
1388
var mapping = last_arg.mapping,
1389
getitem = $B.$getattr(mapping, "__getitem__")
1390
// Get the rest of the arguments
1391
var args = []
1392
for(var i = 0, len = arguments.length - 1; i < len; i++){
1393
args.push(arguments[i])
1394
}
1395
var $ = $B.args("format", 1, {self: null}, ["self"],
1396
args, {}, "$args", null)
1397
}else{
1398
var $ = $B.args("format", 1, {self: null}, ["self"],
1399
arguments, {}, "$args", "$kw"),
1400
mapping = $.$kw, // dictionary
1401
getitem = function(key){
1402
return _b_.dict.$getitem(mapping, key)
1403
}
1404
}
1417
1418
if(fmt.spec !== undefined){
1419
// "spec" may contain "nested replacement fields"
1420
// In this case, evaluate them using the positional
1421
// or keyword arguments passed to format()
1422
function replace_nested(name, key){
1423
if(/\d+/.exec(key)){
1424
// If key is numeric, search in positional
1425
// arguments
1426
return _b_.tuple.__getitem__($.$args,
1427
parseInt(key))
1428
}else{
1429
// Else try in keyword arguments
1430
return _b_.dict.__getitem__($.$kw, key)
1431
}
1432
}
1433
fmt.spec = fmt.spec.replace(/\{(.*?)\}/g,
1434
replace_nested)
1435
}
1437
// Numerical reference : use positional arguments
1438
var pos = parseInt(fmt.name),
1459
// If the conversion flag is set, first call a function to convert
1460
// the value
1461
if(fmt.conv == "a"){value = _b_.ascii(value)}
1462
else if(fmt.conv == "r"){value = _b_.repr(value)}
1463
else if(fmt.conv == "s"){value = _b_.str.$factory(value)}
1476
str.format_map = function(self, mapping){
1477
var $ = $B.args("format_map", 2, {self: null, mapping: null},
1478
['self', 'mapping'], arguments, {}, null, null)
1479
return str.format(self, {$nat: 'mapping', mapping})
1492
/* Return true if the string is empty or all characters in the string are
1493
ASCII, false otherwise. ASCII characters have code points in the range
1494
U+0000-U+007F. */
1503
str.isalnum = function(self){
1504
/* Return true if all characters in the string are alphanumeric and there
1505
is at least one character, false otherwise. A character c is alphanumeric
1506
if one of the following returns True: c.isalpha(), c.isdecimal(),
1507
c.isdigit(), or c.isnumeric(). */
1508
var $ = $B.args("isalnum", 1, {self: null}, ["self"],
1509
arguments, {}, null, null),
1510
cp
1511
for(var char of to_chars(self)){
1512
cp = _b_.ord(char)
1513
if(unicode_tables.Ll[cp] ||
1514
unicode_tables.Lu[cp] ||
1515
unicode_tables.Lm[cp] ||
1516
unicode_tables.Lt[cp] ||
1517
unicode_tables.Lo[cp] ||
1518
unicode_tables.Nd[cp] ||
1519
unicode_tables.digits[cp] ||
1520
unicode_tables.numeric[cp]){
1521
continue
1522
}
1523
return false
1524
}
1525
return true
1526
}
1527
1528
str.isalpha = function(self){
1529
/* Return true if all characters in the string are alphabetic and there is
1530
at least one character, false otherwise. Alphabetic characters are those
1531
characters defined in the Unicode character database as "Letter", i.e.,
1532
those with general category property being one of "Lm", "Lt", "Lu", "Ll",
1533
or "Lo". */
1534
var $ = $B.args("isalpha", 1, {self: null}, ["self"],
1535
arguments, {}, null, null),
1536
cp
1537
for(var char of to_chars(self)){
1538
cp = _b_.ord(char)
1539
if(unicode_tables.Ll[cp] ||
1540
unicode_tables.Lu[cp] ||
1541
unicode_tables.Lm[cp] ||
1542
unicode_tables.Lt[cp] ||
1543
unicode_tables.Lo[cp]){
1544
continue
1545
}
1546
return false
1547
}
1548
return true
1549
}
1550
1551
str.isdecimal = function(self){
1552
/* Return true if all characters in the string are decimal characters and
1553
there is at least one character, false otherwise. Decimal characters are
1554
those that can be used to form numbers in base 10, e.g. U+0660,
1555
ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in
1556
the Unicode General Category "Nd". */
1557
var $ = $B.args("isdecimal", 1, {self: null}, ["self"],
1558
arguments, {}, null, null),
1559
cp
1560
for(var char of to_chars(self)){
1561
cp = _b_.ord(char)
1562
if(! unicode_tables.Nd[cp]){
1563
return false
1564
}
1565
}
1566
return self.length > 0
1567
}
1568
1569
str.isdigit = function(self){
1570
/* Return true if all characters in the string are digits and there is at
1571
least one character, false otherwise. */
1572
var $ = $B.args("isdigit", 1, {self: null}, ["self"],
1573
arguments, {}, null, null),
1574
cp
1575
for(var char of to_chars(self)){
1576
cp = _b_.ord(char)
1577
if(! unicode_tables.digits[cp]){
1578
return false
1579
}
1580
}
1581
return self.length > 0
1582
}
1583
1584
str.isidentifier = function(self){
1585
/* Return true if the string is a valid identifier according to the
1586
language definition. */
1587
var $ = $B.args("isidentifier", 1, {self: null}, ["self"],
1591
}
1592
var chars = to_chars(self)
1593
if(unicode_tables.XID_Start[_b_.ord(chars[0])] === undefined){
1596
for(var char of chars){
1597
var cp = _b_.ord(char)
1598
if(unicode_tables.XID_Continue[cp] === undefined){
1599
return false
1600
}
1601
}
1602
}
1603
return true
1604
}
1605
1606
str.islower = function(self){
1607
/* Return true if all cased characters 4 in the string are lowercase and
1608
there is at least one cased character, false otherwise. */
1609
var $ = $B.args("islower", 1, {self: null}, ["self"],
1610
arguments, {}, null, null),
1611
has_cased = false,
1614
for(var char of to_chars(self)){
1615
cp = _b_.ord(char)
1616
if(unicode_tables.Ll[cp]){
1617
has_cased = true
1618
continue
1619
}else if(unicode_tables.Lu[cp] || unicode_tables.Lt[cp]){
1620
return false
1621
}
1622
}
1623
return has_cased
1624
}
1625
1626
str.isnumeric = function(self){
1627
/* Return true if all characters in the string are numeric characters, and
1628
there is at least one character, false otherwise. Numeric characters
1629
include digit characters, and all characters that have the Unicode numeric
1630
value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric
1631
characters are those with the property value Numeric_Type=Digit,
1632
Numeric_Type=Decimal or Numeric_Type=Numeric.*/
1633
var $ = $B.args("isnumeric", 1, {self: null}, ["self"],
1634
arguments, {}, null, null)
1643
var unprintable = {},
1644
unprintable_gc = ['Cc', 'Cf', 'Co', 'Cs','Zl', 'Zp', 'Zs']
1645
1646
str.isprintable = function(self){
1647
/* Return true if all characters in the string are printable or the string
1648
is empty, false otherwise. Nonprintable characters are those characters
1649
defined in the Unicode character database as "Other" or "Separator",
1650
excepting the ASCII space (0x20) which is considered printable. */
1651
1652
// Set unprintable if not set yet
1653
if(Object.keys(unprintable).length == 0){
1654
for(var i = 0; i < unprintable_gc.length; i++){
1655
var table = unicode_tables[unprintable_gc[i]]
1666
return false
1667
}
1668
}
1669
return true
1670
}
1671
1672
str.isspace = function(self){
1673
/* Return true if there are only whitespace characters in the string and
1674
there is at least one character, false otherwise.
1675
1676
A character is whitespace if in the Unicode character database, either its
1677
general category is Zs ("Separator, space"), or its bidirectional class is
1678
one of WS, B, or S.*/
1679
var $ = $B.args("isspace", 1, {self: null}, ["self"],
1680
arguments, {}, null, null),
1681
cp
1682
for(var char of to_chars(self)){
1683
cp = _b_.ord(char)
1684
if(! unicode_tables.Zs[cp] &&
1685
$B.unicode_bidi_whitespace.indexOf(cp) == -1){
1686
return false
1687
}
1688
}
1689
return self.length > 0
1690
}
1691
1692
str.istitle = function(self){
1693
/* Return true if the string is a titlecased string and there is at least
1694
one character, for example uppercase characters may only follow uncased
1695
characters and lowercase characters only cased ones. Return false
1696
otherwise. */
1697
var $ = $B.args("istitle", 1, {self: null}, ["self"],
1698
arguments, {}, null, null)
1699
return self.length > 0 && str.title(self) == self
1700
}
1701
1702
str.isupper = function(self){
1703
/* Return true if all cased characters 4 in the string are lowercase and
1704
there is at least one cased character, false otherwise. */
1705
var $ = $B.args("islower", 1, {self: null}, ["self"],
1706
arguments, {}, null, null),
1710
for(var char of to_chars(self)){
1711
cp = _b_.ord(char)
1712
if(unicode_tables.Lu[cp]){
1713
is_upper = true
1714
continue
1715
}else if(unicode_tables.Ll[cp] || unicode_tables.Lt[cp]){
1734
throw _b_.TypeError.$factory("sequence item " + count +
1735
": expected str instance, " + $B.class_name(obj2) +
1736
" found")
1737
}
1750
var $ = $B.args("ljust", 3, {self: null, width: null, fillchar:null},
1751
["self", "width", "fillchar"],
1755
if($.width <= len){
1756
return self
1757
}
1758
return self + $.fillchar.repeat($.width - len)
1761
str.lower = function(self){
1762
var $ = $B.args("lower", 1, {self: null}, ["self"],
1763
arguments, {}, null, null)
1764
return self.toLowerCase()
1765
}
1766
1768
var $ = $B.args("lstrip", 2, {self: null, chars: null}, ["self", "chars"],
1769
arguments, {chars:_b_.None}, null, null)
1770
if($.chars === _b_.None){
1771
return $.self.trimLeft()
1772
}
1773
var chars = to_chars(self)
1774
for(var i = 0, len = chars.length; i < len; i++){
1775
if($.chars.indexOf(chars[i]) === -1){
1776
return chars.slice(i).join('')
1784
var $ = $B.args("maketrans", 3, {x: null, y: null, z: null},
1785
["x", "y", "z"], arguments, {y: null, z: null}, null, null)
1790
// If there is only one argument, it must be a dictionary mapping
1791
// Unicode ordinals (integers) or characters (strings of length 1) to
1792
// Unicode ordinals, strings (of arbitrary lengths) or None. Character
1794
if(! _b_.isinstance($.x, _b_.dict)){
1795
throw _b_.TypeError.$factory(
1796
"maketrans only argument must be a dict")
1799
for(var i = 0, len = items.length; i < len; i++){
1800
var k = items[i][0],
1801
v = items[i][1]
1802
if(! _b_.isinstance(k, _b_.int)){
1803
if(_b_.isinstance(k, _b_.str) && k.length == 1){
1804
k = _b_.ord(k)
1805
}else{throw _b_.TypeError.$factory("dictionary key " + k +
1808
if(v !== _b_.None && ! _b_.isinstance(v, [_b_.int, _b_.str])){
1809
throw _b_.TypeError.$factory("dictionary value " + v +
1817
// and in the resulting dictionary, each character in x will be mapped
1818
// to the character at the same position in y
1821
}else if($.x.length !== $.y.length){
1822
throw _b_.TypeError.$factory(
1823
"maketrans arguments must be strings or same length")
1829
if(! _b_.isinstance($.z, _b_.str)){
1830
throw _b_.TypeError.$factory(
1831
"maketrans third argument must be a string")
1853
var $ = $B.args("partition", 2, {self: null, sep: null}, ["self", "sep"],
1854
arguments, {}, null, null)
1861
if(i == -1){
1862
return _b_.tuple.$factory([$.self, "", ""])
1863
}
1864
return _b_.tuple.$factory([chars.slice(0, i).join(''), $.sep,
1865
chars.slice(i + $.sep.length).join('')])
1868
str.removeprefix = function(){
1869
var $ = $B.args("removeprefix", 2, {self: null, prefix: null},
1870
["self", "prefix"], arguments, {}, null, null)
1871
if(!_b_.isinstance($.prefix, str)){
1872
throw _b_.ValueError.$factory("prefix should be str, not " +
1873
`'${$B.class_name($.prefix)}'`)
1874
}
1875
if(str.startswith($.self, $.prefix)){
1876
return $.self.substr($.prefix.length)
1877
}
1878
return $.self.substr(0)
1879
}
1880
1881
str.removesuffix = function(){
1882
var $ = $B.args("removesuffix", 2, {self: null, prefix: null},
1883
["self", "suffix"], arguments, {}, null, null)
1884
if(!_b_.isinstance($.suffix, str)){
1885
throw _b_.ValueError.$factory("suffix should be str, not " +
1886
`'${$B.class_name($.prefix)}'`)
1887
}
1888
if($.suffix.length > 0 && str.endswith($.self, $.suffix)){
1889
return $.self.substr(0, $.self.length - $.suffix.length)
1890
}
1891
return $.self.substr(0)
1892
}
1893
1894
function $re_escape(str){
1895
var specials = "[.*+?|()$^"
1896
for(var i = 0, len = specials.length; i < len; i++){
1897
var re = new RegExp("\\"+specials.charAt(i), "g")
1898
str = str.replace(re, "\\"+specials.charAt(i))
1899
}
1900
return str
1907
var $ = $B.args("replace", 4,
1908
{self: null, old: null, $$new: null, count: null},
1909
["self", "old", "$$new", "count"],
1910
arguments, {count: -1}, null, null),
1911
count = $.count,
1912
self = $.self,
1913
old = $.old,
1914
_new = $.$$new
1916
check_str(old, "replace() argument 1 ")
1917
check_str(_new, "replace() argument 2 ")
1925
if(count == 0){
1926
return self
1927
}
1928
if(count.__class__ == $B.long_int){
1929
count = parseInt(count.value)
1930
}
1932
if(_new == ""){
1933
return self
1934
}
1935
if(self == ""){
1936
return _new
1937
}
1938
var elts = self.split("")
1939
if(count > -1 && elts.length >= count){
1940
var rest = elts.slice(count).join("")
1941
return _new + elts.slice(0, count).join(_new) + rest
1967
res = res.substr(0, pos) + _new + res.substr(pos + old.length)
1968
pos = pos + _new.length
1969
count--
1975
// Return the highest index in the string where substring sub is found,
1976
// such that sub is contained within s[start:end]. Optional arguments
1978
if(arguments.length == 2 && typeof substr == "string"){
1979
return self.lastIndexOf(substr)
1980
}
1982
{self: null, sub: null, start: null, end: null},
1983
["self", "sub", "start", "end"],
1984
arguments, {start: 0, end: null}, null, null)
1991
if($.start > $.self.length){
1992
return -1
1993
}else{
1994
return str.__len__($.self)
1995
}
2001
if($.self.substr(i, sublen) == $.sub){
2002
return str.__len__($.self.substr(0, i))
2003
}
2018
var $ = $B.args("rjust",3,
2019
{self: null, width: null, fillchar: null},
2020
["self", "width", "fillchar"],
2021
arguments, {fillchar: " "}, null, null)
2031
var $ = $B.args("rpartition", 2, {self: null, sep: null}, ["self", "sep"],
2032
arguments, {}, null, null)
2036
var items = str.partition(self, sep).reverse()
2037
for(var i = 0; i < items.length; i++){
2038
items[i] = items[i].split("").reverse().join("")
2044
var $ = $B.args("rsplit", 3, {self: null, sep: null, maxsplit: null},
2045
["self", "sep", "maxsplit"], arguments,
2046
{sep: _b_.None, maxsplit: -1}, null, null),
2047
sep = $.sep
2050
var rev_str = reverse($.self),
2051
rev_sep = sep === _b_.None ? sep : reverse($.sep),
2062
str.rstrip = function(self, x){
2063
var $ = $B.args("rstrip", 2, {self: null, chars: null}, ["self", "chars"],
2065
if($.chars === _b_.None){
2066
return $.self.trimRight()
2067
}
2068
var chars = to_chars(self)
2069
for(var j = chars.length - 1; j >= 0; j--){
2070
if($.chars.indexOf(chars[j]) == -1){
2071
return chars.slice(0, j + 1).join('')
2078
var $ = $B.args("split", 3, {self: null, sep: null, maxsplit: null},
2079
["self", "sep", "maxsplit"], arguments,
2080
{sep: _b_.None, maxsplit: -1}, null, null),
2081
sep = $.sep,
2082
maxsplit = $.maxsplit,
2083
self = $.self,
2084
pos = 0
2085
if(maxsplit.__class__ === $B.long_int){
2086
maxsplit = parseInt(maxsplit.value)
2087
}
2088
if(sep == ""){
2089
throw _b_.ValueError.$factory("empty separator")
2090
}
2093
while(pos < self.length && self.charAt(pos).search(/\s/) > -1){
2094
pos++
2095
}
2096
if(pos === self.length - 1){
2097
return [self]
2098
}
2102
if(name == ""){
2103
name = self.charAt(pos)
2104
}else{
2105
name += self.charAt(pos)
2106
}
2128
var res = [],
2129
s = "",
2130
seplen = sep.length
2131
if(maxsplit == 0){return [self]}
2132
while(pos < self.length){
2133
if(self.substr(pos, seplen) == sep){
2151
str.splitlines = function(self) {
2152
var $ = $B.args('splitlines', 2, {self: null, keepends: null},
2153
['self','keepends'], arguments, {keepends: false},
2154
null, null)
2155
if(!_b_.isinstance($.keepends,[_b_.bool, _b_.int])){
2156
throw _b_.TypeError('integer argument expected, got '+
2159
var keepends = _b_.int.$factory($.keepends),
2160
res = [],
2161
self = $.self,
2162
start = 0,
2163
pos = 0
2164
if(!self.length){
2168
if(self.substr(pos, 2) == '\r\n'){
2169
res.push(self.slice(start, keepends ? pos + 2 : pos))
2170
start = pos = pos+2
2171
}else if(self[pos] == '\r' || self[pos] == '\n'){
2172
res.push(self.slice(start, keepends ? pos+1 : pos))
2173
start = pos = pos+1
2174
}else{
2175
pos++
2176
}
2177
}
2178
if(start < self.length){
2179
res.push(self.slice(start))
2180
}
2181
return res
2185
// Return True if string starts with the prefix, otherwise return False.
2186
// prefix can also be a tuple of prefixes to look for. With optional
2187
// start, test string beginning at that position. With optional end,
2189
var $ = $B.args("startswith", 4,
2190
{self: null, prefix: null, start: null, end: null},
2191
["self", "prefix", "start", "end"],
2192
arguments, {start: 0, end: null}, null, null)
2204
if(! _b_.isinstance(prefix, str)){
2205
throw _b_.TypeError.$factory("endswith first arg must be str " +
2206
"or a tuple of str, not int")
2207
}
2208
if(s.substr(0, prefix.length) == prefix){
2209
return true
2210
}
2216
var $ = $B.args("strip", 2, {self: null, chars: null}, ["self", "chars"],
2217
arguments, {chars: _b_.None}, null, null)
2218
if($.chars === _b_.None){
2219
return $.self.trim()
2220
}
2221
var chars = to_chars($.self)
2222
for(var i = 0; i < chars.length; i++){
2223
if($.chars.indexOf(chars[i]) == -1){
2227
for(var j = chars.length - 1; j >= i; j--){
2228
if($.chars.indexOf(chars[j]) == -1){
2235
str.swapcase = function(self){
2236
var $ = $B.args("swapcase", 1, {self}, ["self"],
2237
arguments, {}, null, null),
2238
res = "",
2239
cp
2240
2241
for(var char of to_chars(self)){
2242
cp = _b_.ord(char)
2243
if(unicode_tables.Ll[cp]){
2244
res += char.toUpperCase()
2245
}else if(unicode_tables.Lu[cp]){
2246
res += char.toLowerCase()
2249
}
2250
}
2251
return res
2252
}
2253
2254
str.title = function(self){
2255
var $ = $B.args("title", 1, {self}, ["self"],
2256
arguments, {}, null, null),
2257
state,
2260
for(var char of to_chars(self)){
2261
cp = _b_.ord(char)
2262
if(unicode_tables.Ll[cp]){
2269
}else if(unicode_tables.Lu[cp] || unicode_tables.Lt[cp]){
2270
res += state ? char.toLowerCase() : char
2282
getitem = $B.$getattr(table, "__getitem__"),
2283
cp
2284
for(var char of to_chars(self)){
2285
cp = _b_.ord(char)
2289
if(typeof repl == "string"){
2290
res.push(repl)
2291
}else if(typeof repl == "number"){
2292
res.push(String.fromCharCode(repl))
2293
}
2302
str.upper = function(self){
2303
var $ = $B.args("upper", 1, {self: null}, ["self"],
2304
arguments, {}, null, null)
2305
return self.toUpperCase()
2306
}
2307
2310
["self", "width"], arguments, {}, null, null),
2311
len = str.__len__(self)
2312
if($.width <= len){
2313
return self
2314
}
2334
if(encoding !== undefined){
2335
// Arguments may be passed as keywords (cf. issue #1060)
2336
var $ = $B.args("str", 3, {arg: null, encoding: null, errors: null},
2337
["arg", "encoding", "errors"], arguments,
2338
{encoding: "utf-8", errors: "strict"}, null, null),
2339
encoding = $.encoding,
2340
errors = $.errors
2341
}
2352
// class or its subclasses, but the attribute __str__ of the
2353
// class metaclass (usually "type") or its subclasses (usually
2354
// "object")
2355
// The metaclass is the attribute __class__ of the class dictionary
2360
if(arg.__class__ && arg.__class__ === _b_.bytes &&
2361
encoding !== undefined){
2362
// str(bytes, encoding, errors) is equal to
2363
// bytes.decode(encoding, errors)
2366
// Implicit invocation of __str__ uses method __str__ on the class,
2367
// even if arg has an attribute __str__
2368
var klass = arg.__class__ || $B.get_class(arg)
2374
// if not better than object.__str__, try __repr__
2375
(arg.__class__ && arg.__class__ !== _b_.object &&
2376
method.$infos && method.$infos.__func__ === _b_.object.__str__)){
2377
var method = $B.$getattr(klass, "__repr__")
2383
if($B.debug > 1){console.log(err)}
2384
console.log("Warning - no method __str__ or __repr__, " +
2385
"default to toString", arg)
2392
if(cls === undefined){
2393
throw _b_.TypeError.$factory("str.__new__(): not enough arguments")
2417
var args = [],
2418
pos = 0
2419
if(arguments.length > 0){
2420
var args = [arguments[0].valueOf()],
2421
pos = 1
2422
for(var i = 1, len = arguments.length; i < len; i++){
2423
args[pos++] = arguments[i]
2440
// Function to parse the 2nd argument of format()
2441
$B.parse_format_spec = function(spec){
2445
var pos = 0,
2446
aligns = "<>=^",
2447
digits = "0123456789",
2448
types = "bcdeEfFgGnosxX%",
2450
if(align_pos != -1){
2451
if(spec.charAt(1) && aligns.indexOf(spec.charAt(1)) != -1){
2452
// If the second char is also an alignment specifier, the
2453
// first char is the fill value
2454
this.fill = spec.charAt(0)
2455
this.align = spec.charAt(1)
2456
pos = 2
2457
}else{
2458
// The first character defines alignment : fill defaults to ' '
2473
if(car == "+" || car == "-" || car == " "){
2474
this.sign = car
2475
pos++
2476
car = spec.charAt(pos)
2491
if(this.width === undefined){
2492
this.width = car
2493
}else{
2494
this.width += car
2495
}
2502
if(this.width === undefined && car == "{"){
2503
// Width is determined by a parameter
2504
var end_param_pos = spec.substr(pos).search("}")
2505
this.width = spec.substring(pos, end_param_pos)
2506
console.log("width", "[" + this.width + "]")
2507
pos += end_param_pos + 1
2508
}
2509
if(car == ","){
2510
this.comma = true
2511
pos++
2512
car = spec.charAt(pos)
2513
}
2514
if(car == "."){
2515
if(digits.indexOf(spec.charAt(pos + 1)) == -1){
2516
throw _b_.ValueError.$factory(
2517
"Missing precision in format spec")
2519
this.precision = spec.charAt(pos + 1)
2520
pos += 2
2521
car = spec.charAt(pos)
2522
while(car && digits.indexOf(car) > -1){
2529
if(car && types.indexOf(car) > -1){
2530
this.type = car
2531
pos++
2532
car = spec.charAt(pos)
2533
}
2534
if(pos !== spec.length){
2540
return (this.fill === undefined ? "" : _b_.str.$factory(this.fill)) +
2541
(this.align || "") +
2542
(this.sign || "") +
2543
(this.alternate ? "#" : "") +
2544
(this.sign_aware ? "0" : "") +
2545
(this.width || "") +
2546
(this.comma ? "," : "") +
2547
(this.precision ? "." + this.precision : "") +
2548
(this.type || "")
2553
if(fmt.width && s.length < fmt.width){
2554
var fill = fmt.fill || " ",
2555
align = fmt.align || "<",
2556
missing = fmt.width - s.length
2558
case "<":
2559
return s + fill.repeat(missing)
2560
case ">":
2561
return fill.repeat(missing) + s
2562
case "=":
2563
if("+-".indexOf(s.charAt(0)) > -1){
2564
return s.charAt(0) + fill.repeat(missing) + s.substr(1)
2568
case "^":
2569
var left = parseInt(missing / 2)
2570
return fill.repeat(left) + s + fill.repeat(missing - left)
2583
function fstring_error(msg, pos){
2584
error = Error(msg)
2585
error.position = pos
2586
throw error
2587
}
2588
2589
$B.parse_fstring = function(string){
2590
// Parse a f-string
2591
var elts = [],
2592
pos = 0,
2654
}else if(ctype == "debug"){
2655
// after the equal sign, whitespace are ignored and the only
2656
// valid characters are } and :
2657
while(string.charAt(i) == " "){i++}
2658
if(string.charAt(i) == "}"){
2659
// end of debug expression
2660
elts.push(current)
2661
ctype = null
2662
current = ""
2663
pos = i + 1
2664
}
2665
}else{
2666
// End of expression is the } matching the opening {
2667
// There may be nested braces
2668
var i = pos,
2669
nb_braces = 1,
2682
if(current.expression == ""){
2683
fstring_error("f-string: empty expression not allowed",
2684
pos)
2685
}
2695
// backslash is not allowed in expressions
2696
throw Error("f-string expression part cannot include a" +
2697
" backslash")
2704
throw Error("f-string: invalid conversion character:" +
2705
" expected 's', 'r', or 'a'")
2706
}else{
2720
if(string.substr(i, 3) == '"""'){
2721
var end = string.indexOf('"""', i + 3)
2722
if(end == -1){
2724
}else{
2725
var trs = string.substring(i, end + 3)
2726
trs = trs.replace("\n", "\\n\\")
2745
var ce = current.expression,
2746
last_char = ce.charAt(ce.length - 1),
2747
last_char_re = ('()'.indexOf(last_char) > -1 ? "\\" : "") + last_char
2752
"=!<>:".search(last_char_re) > -1){
2753
current.expression += car //+ string.charAt(i + 1)
2754
i += 1
2755
}else{
2756
// add debug string
2757
tail = car
2758
while(string.charAt(i + 1).match(/\s/)){
2759
tail += string.charAt(i + 1)
2760
i++
2761
}
2762
elts.push(current.expression + tail)
2763
// remove trailing whitespace from expression
2764
while(ce.match(/\s$/)){
2765
ce = ce.substr(0, ce.length - 1)
2766
}
2767
current.expression = ce
2768
ctype = "debug"
2769
i++
2770
}
2786
if(i >= 0x10000 && i <= 0x10FFFF){
2787
var code = (i - 0x10000)
2788
return String.fromCodePoint(0xD800 | (code >> 10)) +
2789
String.fromCodePoint(0xDC00 | (code & 0x3FF))
2790
}else{
2791
return String.fromCodePoint(i)
2792
}
2793
}
2796
if(c.length == 1){
2797
return c.charCodeAt(0)
2798
}
2799
var code = 0x10000
2800
code += (c.charCodeAt(0) & 0x03FF) << 10
2801
code += (c.charCodeAt(1) & 0x03FF)
2802
return code
2803
}
2804