Permalink
Mar 6, 2021
Mar 19, 2018
Mar 19, 2018
Mar 6, 2021
Mar 19, 2018
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 6, 2021
Mar 6, 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
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 6, 2021
Mar 19, 2018
Mar 19, 2018
Mar 27, 2019
Mar 6, 2021
Mar 19, 2018
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
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
Mar 19, 2018
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
2759 lines (2549 sloc)
83.7 KB
9
10
$B.has_surrogate = function(s){
11
// Check if there are "surrogate pairs" characters in string s
12
for(var i = 0; i < s.length; i++){
13
code = s.charCodeAt(i)
14
if(code >= 0xD800 && code <= 0xDBFF){
15
return true
16
}
17
}
18
return false
19
}
20
33
if($.start === null || $.start === _b_.None){$.start = 0}
34
else if($.start < 0){
35
$.start += $.self.length
36
$.start = Math.max(0, $.start)
37
}
38
if($.end === null || $.end === _b_.None){$.end = $.self.length}
39
else if($.end < 0){
40
$.end += $.self.length
41
$.end = Math.max(0, $.end)
42
}
44
if(! isinstance($.start, _b_.int) || ! isinstance($.end, _b_.int)){
45
throw _b_.TypeError.$factory("slice indices must be integers " +
46
"or None or have an __index__ method")
47
}
62
function to_chars(s){
63
// Transform Javascript string s into a list of Python characters
64
// (2 JS chars if surrogate, 1 otherwise)
65
var chars = []
66
for(var i = 0, len = s.length; i < len; i++){
67
var code = s.charCodeAt(i)
68
if(code >= 0xD800 && code <= 0xDBFF){
69
chars.push(s.substr(i, 2))
70
i++
71
}else{
72
chars.push(s.charAt(i))
73
}
74
}
75
return chars
76
}
77
79
if(!(typeof other === "string")){
80
try{return getattr(other, "__radd__")(self)}
81
catch(err){
82
throw _b_.TypeError.$factory("Can't convert " +
90
throw _b_.TypeError.$factory("'in <string>' requires " +
91
"string as left operand, not " + item.__class__)
92
}
93
if(typeof item == "string"){
94
var nbcar = item.length
95
}else{
96
var nbcar = _b_.len(item)
97
}
98
if(nbcar == 0){
99
// a string contains the empty string
100
return true
101
}
102
if(self.length == 0){
103
return nbcar == 0
104
}
117
// __dir__must be assigned explicitely because attribute resolution for
118
// builtin classes doesn't use __mro__
130
if(fmt.type && fmt.type != "s"){
131
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
139
if(fmt.sign !== undefined){
140
throw _b_.ValueError.$factory(
141
"Sign not allowed in string format specifier")
155
if(arg < 0){
156
pos += self.length
157
}
158
if(pos >= 0 && pos < chars.length){
159
return chars[pos]
160
}
164
var s = _b_.slice.$conv_for_seq(arg, self.length),
165
start = s.start,
166
stop = s.stop,
167
step = s.step
168
var res = "",
171
if(stop <= start){
172
return ""
173
}
174
for(var i = start; i < stop; i += step){
175
res += chars[i]
176
}
178
if(stop >= start){
179
return ''
180
}
181
for(var i = start; i > stop; i += step){
182
res += chars[i]
183
}
187
if(isinstance(arg, _b_.bool)){
188
return self.__getitem__(_b_.int.$factory(arg))
189
}
244
var len = self.length,
245
res = len,
246
code
247
for(var i = 0; i < len; i++){
248
code = self.charCodeAt(i)
249
if(code >= 0xD800 && code <= 0xDBFF){
250
res--
251
}
252
}
253
return res
290
// left adjusted
291
return s + get_char_array(padding - s.length, flags.pad_char)
292
}
293
}
294
302
if(val.__class__ === $B.long_int){
303
s = $B.long_int.to_base(val, 10)
304
}else{
305
s = val.toString()
307
if(s[0] === "-"){
308
return "-" + get_char_array(precision - s.length + 1, "0") + s.slice(1)
319
if(val === Infinity){
320
val = "inf"
321
}else if(val === -Infinity){
322
val = "-inf"
323
}else{
324
val = "nan"
346
var str_format = function(val, flags) {
347
// string format supports left and right padding
348
flags.pad_char = " " // even if 0 padding is defined, don't use it
354
if(val.__class__ === $B.long_int){
355
val = $B.long_int.to_base(val, 10)
356
}else{
357
val = parseInt(val)
375
var repr_format = function(val, flags) {
376
flags.pad_char = " " // even if 0 padding is defined, don't use it
377
return format_padding(repr(val), flags)
378
}
380
var ascii_format = function(val, flags) {
381
flags.pad_char = " " // even if 0 padding is defined, don't use it
382
return format_padding(ascii(val), flags)
383
}
395
flags.precision = parseInt(flags.precision, 10)
396
validate_precision(flags.precision)
397
}
398
return parseFloat(val)
399
}
402
var trailing_zeros = /(.*?)(0+)([eE].*)/,
403
leading_zeros = /\.(0*)/,
404
trailing_dot = /\.$/
406
var validate_precision = function(precision) {
407
// force precision to limits of javascript
412
var floating_point_format = function(val, upper, flags){
413
val = _float_helper(val, flags),
414
v = val.toString(),
415
v_len = v.length,
416
dot_idx = v.indexOf('.')
417
if(dot_idx < 0){dot_idx = v_len}
418
if(val < 1 && val > -1){
419
var zeros = leading_zeros.exec(v),
420
numzeros
421
if(zeros){
426
if(numzeros >= 4){
427
val = format_sign(val, flags) + format_float_precision(val, upper,
428
flags, _floating_g_exp_helper)
429
if(!flags.alternate){
442
return format_padding(format_sign(val, flags) +
443
format_float_precision(val, upper, flags,
444
function(val, precision) {
445
return val.toFixed(min(precision, v_len - dot_idx) +
446
numzeros)
447
}),
448
flags
449
)
450
}
451
452
if(dot_idx > flags.precision){
453
val = format_sign(val, flags) + format_float_precision(val, upper,
454
flags, _floating_g_exp_helper)
455
if(! flags.alternate){
467
return format_padding(format_sign(val, flags) +
468
format_float_precision(val, upper, flags,
469
function(val, precision) {
470
if(!flags.decimal_point){
471
precision = min(v_len - 1, 6)
472
}else if (precision > v_len){
473
if(! flags.alternate){
474
precision = v_len
475
}
477
if(precision < dot_idx){
478
precision = dot_idx
479
}
480
return val.toFixed(precision - dot_idx)
481
}),
482
flags
483
)
486
var _floating_g_exp_helper = function(val, precision, flags, upper){
487
if(precision){--precision}
490
var e_idx = val.lastIndexOf("e")
491
if(e_idx > val.length - 4){
492
val = val.substring(0, e_idx + 2) + "0" + val.substring(e_idx + 2)
495
return val
496
}
497
498
// fF
499
var floating_point_decimal_format = function(val, upper, flags) {
500
val = _float_helper(val, flags)
501
return format_padding(format_sign(val, flags) +
502
format_float_precision(val, upper, flags,
503
function(val, precision, flags) {
504
val = val.toFixed(precision)
505
if(precision === 0 && flags.alternate){
506
val += '.'
507
}
508
return val
509
}),
510
flags
511
)
512
}
513
514
var _floating_exp_helper = function(val, precision, flags, upper) {
515
val = val.toExponential(precision)
516
// pad exponent to two digits
529
return format_padding(format_sign(val, flags) +
530
format_float_precision(val, upper, flags, _floating_exp_helper), flags)
556
if(flags.alternate){
557
if(ret.charAt(0) === "-"){
558
if(upper){ret = "-0X" + ret.slice(1)}
559
else{ret = "-0x" + ret.slice(1)}
560
}else{
561
if(upper){ret = "0X" + ret}
562
else{ret = "0x" + ret}
572
if(val.__class__ === $B.long_int){
573
ret = $B.long_int.to_base(8)
574
}else{
575
ret = parseInt(val)
576
ret = ret.toString(8)
592
if(flags.alternate){
593
if(ret.charAt(0) === "-"){ret = "-0o" + ret.slice(1)}
594
else{ret = "0o" + ret}
599
function series_of_bytes(val, flags){
600
if(val.__class__ && val.__class__.$buffer_protocol){
601
var it = _b_.iter(val),
602
ints = []
603
while(true){
604
try{
605
ints.push(_b_.next(it))
606
}catch(err){
607
if(err.__class__ === _b_.StopIteration){
608
var b = _b_.bytes.$factory(ints)
609
return format_padding(_b_.bytes.decode(b, "ascii"), flags)
610
}
611
throw err
612
}
613
}
614
}else{
615
try{
616
bytes_obj = $B.$getattr(val, "__bytes__")
617
return format_padding(_b_.bytes.decode(bytes_obj), flags)
618
}catch(err){
619
if(err.__class__ === _b_.AttributeError){
620
throw _b_.TypeError.$factory("%b does not accept '" +
621
$B.class_name(val) + "'")
622
}
623
throw err
624
}
625
}
626
}
627
629
if(isinstance(val, str) && val.length == 1){
630
return val
631
}else if(isinstance(val, bytes) && val.source.length == 1){
632
val = val.source[0]
633
}else{
634
try{
635
val = _b_.int.$factory(val) // yes, floats are valid (they are cast to int)
636
}catch (err){
637
throw _b_.TypeError.$factory("%c requires int or char")
638
}
643
var num_flag = function(c, flags){
644
if(c === "0" && ! flags.padding && ! flags.decimal_point && ! flags.left){
645
flags.pad_char = "0"
651
flags.precision = (flags.precision || "") + c
652
}
653
}
654
655
var decimal_point_flag = function(val, flags) {
657
// can only have one decimal point
658
throw new UnsupportedChar()
659
}
660
flags.decimal_point = true
661
}
662
663
var neg_flag = function(val, flags){
664
flags.pad_char = " " // overrides '0' flag
682
"s": str_format,
683
"d": num_format,
684
"i": num_format,
685
"u": num_format,
686
"o": octal_format,
687
"r": repr_format,
688
"a": ascii_format,
689
"g": function(val, flags){
690
return floating_point_format(val, false, flags)
691
},
692
"G": function(val, flags){return floating_point_format(val, true, flags)},
693
"f": function(val, flags){
694
return floating_point_decimal_format(val, false, flags)
695
},
696
"F": function(val, flags){
697
return floating_point_decimal_format(val, true, flags)
698
},
699
"e": function(val, flags){
700
return floating_point_exponential_format(val, false, flags)
701
},
702
"E": function(val, flags){
703
return floating_point_exponential_format(val, true, flags)
704
},
705
"x": function(val, flags){return signed_hex_format(val, false, flags)},
706
"X": function(val, flags){return signed_hex_format(val, true, flags)},
707
"c": single_char_format,
708
"0": function(val, flags){return num_flag("0", flags)},
709
"1": function(val, flags){return num_flag("1", flags)},
710
"2": function(val, flags){return num_flag("2", flags)},
711
"3": function(val, flags){return num_flag("3", flags)},
712
"4": function(val, flags){return num_flag("4", flags)},
713
"5": function(val, flags){return num_flag("5", flags)},
714
"6": function(val, flags){return num_flag("6", flags)},
715
"7": function(val, flags){return num_flag("7", flags)},
716
"8": function(val, flags){return num_flag("8", flags)},
717
"9": function(val, flags){return num_flag("9", flags)},
718
"-": neg_flag,
719
" ": space_flag,
720
"+": sign_flag,
721
".": decimal_point_flag,
722
"#": alternate_flag
723
}
724
725
// exception thrown when an unsupported char is encountered in legacy format
771
if(self === undefined){
772
throw _b_.TypeError.$factory(
773
"not enough arguments for format string")
800
throw _b_.ValueError.$factory(
801
"unsupported format character '" + invalid_char +
802
"' (0x" + invalid_char.charCodeAt(0).toString(16) +
803
") at index " + newpos)
804
}else if(err.name === "NotANumber"){
805
var try_char = s[newpos],
806
cls = self.__class__
807
if(!cls){
808
if(typeof(self) === "string"){
809
cls = "str"
810
}else{
816
throw _b_.TypeError.$factory("%" + try_char +
817
" format: a number is required, not " + cls)
818
}else{
850
}while(pos < length)
851
852
if(argpos !== null){
853
if(args.length > argpos){
854
throw _b_.TypeError.$factory(
855
"not enough arguments for format string")
856
}else if(args.length < argpos){
857
throw _b_.TypeError.$factory(
858
"not all arguments converted during string formatting")
860
}else if(nbph == 0){
861
throw _b_.TypeError.$factory(
862
"not all arguments converted during string formatting")
870
var $ = $B.args("__mul__", 2, {self: null, other: null},
871
["self", "other"], arguments, {}, null, null)
872
if(! isinstance($.other, _b_.int)){throw _b_.TypeError.$factory(
873
"Can't multiply sequence by non-int of type '" +
875
var res = ""
876
for(var i = 0; i < $.other; i++){
877
res += $.self.valueOf()
878
}
879
return res
888
var t = {
889
8: "\\x08",
890
9: "\\t",
891
10: "\\n",
892
11: "\\x0b",
893
12: "\\x0c",
894
13: "\\r",
895
92: "\\\\"
896
}
897
var repl = '',
898
chars = to_chars(self)
899
for(var i = 0; i < chars.length; i++){
900
var cp = _b_.ord(chars[i])
901
if(t[cp] !== undefined){
902
repl += t[cp]
903
}else if($B.is_unicode_cn(cp)){
922
if(res.search('"') == -1 && res.search("'") == -1){
923
return "'" + res + "'"
924
}else if(self.search('"') == -1){
925
return '"' + res + '"'
926
}
927
var qesc = new RegExp("'", "g") // to escape single quote
928
res = "'" + res.replace(qesc, "\\'") + "'"
932
str.__setitem__ = function(self, attr, value){
933
throw _b_.TypeError.$factory(
934
"'str' object does not support item assignment")
937
var combining = []
938
for(var cp = 0x300; cp <= 0x36F; cp++){
939
combining.push(String.fromCharCode(cp))
940
}
944
var repl = '',
945
chars = to_chars(self)
946
if(chars.length == self.length){
947
return self.replace(combining_re, "\u200B$1")
948
}
949
for(var i = 0; i < chars.length; i++){
950
var cp = _b_.ord(chars[i])
951
if(cp >= 0x300 && cp <= 0x36F){
952
repl += "\u200B" + chars[i]
953
}else{
954
repl += chars[i]
955
}
956
}
957
return repl
967
$comp_func += "" // source code
968
var $comps = {">": "gt", ">=": "ge", "<": "lt", "<=": "le"}
977
var $notimplemented = function(self, other){
978
throw NotImplementedError.$factory(
979
"OPERATOR not implemented for class str")
982
str.capitalize = function(self){
983
var $ = $B.args("capitalize", 1, {self}, ["self"],
984
arguments, {}, null, null)
988
return self.charAt(0).toUpperCase() + self.substr(1)
989
}
990
991
str.casefold = function(self){
992
var $ = $B.args("casefold", 1, {self}, ["self"],
993
arguments, {}, null, null),
994
res = "",
995
char,
996
cf,
997
chars = to_chars($.self)
998
999
for(var i = 0, len = chars.length; i < len; i++){
1000
char = chars[i]
1001
cf = $B.unicode_casefold[char]
1002
if(cf){
1003
cf.forEach(function(cp){
1004
res += String.fromCharCode(cp)
1005
})
1006
}else{
1013
str.center = function(){
1014
var $ = $B.args("center", 3, {self: null, width: null, fillchar: null},
1015
["self", "width", "fillchar"],
1016
arguments, {fillchar:" "}, null, null),
1017
self = $.self
1021
var pad = parseInt(($.width - self.length) / 2),
1022
res = $.fillchar.repeat(pad)
1031
var $ = $B.args("count", 4, {self:null, sub:null, start:null, stop:null},
1032
["self", "sub", "start", "stop"], arguments, {start:null, stop:null},
1034
if(!(typeof $.sub == "string")){
1035
throw _b_.TypeError.$factory("Can't convert '" + $B.class_name($.sub) +
1036
"' object to str implicitly")
1037
}
1041
if($.stop !== null){
1042
_slice = _b_.slice.$factory($.start, $.stop)
1043
}else{
1044
_slice = _b_.slice.$factory($.start, $.self.length)
1045
}
1053
if($.start == $.self.length){
1054
return 1
1055
}else if(substr.length == 0){
1056
return 0
1057
}
1060
var n = 0,
1061
pos = 0
1062
while(pos < substr.length){
1063
pos = substr.indexOf($.sub, pos)
1074
str.encode = function(){
1075
var $ = $B.args("encode", 3, {self: null, encoding: null, errors: null},
1076
["self", "encoding", "errors"], arguments,
1077
{encoding: "utf-8", errors: "strict"}, null, null)
1078
if($.encoding == "rot13" || $.encoding == "rot_13"){
1081
for(var i = 0, len = $.self.length; i < len ; i++){
1082
var char = $.self.charAt(i)
1083
if(("a" <= char && char <= "m") || ("A" <= char && char <= "M")){
1084
res += String.fromCharCode(String.charCodeAt(char) + 13)
1085
}else if(("m" < char && char <= "z") ||
1086
("M" < char && char <= "Z")){
1087
res += String.fromCharCode(String.charCodeAt(char) - 13)
1096
// Return True if the string ends with the specified suffix, otherwise
1097
// return False. suffix can also be a tuple of suffixes to look for.
1098
// With optional start, test beginning at that position. With optional
1102
["self", "suffix", "start", "end"],
1103
arguments, {start: 0, end: null}, null, null)
1116
if(! _b_.isinstance(suffix, str)){
1117
throw _b_.TypeError.$factory(
1118
"endswith first arg must be str or a tuple of str, not int")
1119
}
1129
var $ = $B.args("expandtabs", 2, {self: null, tabsize: null},
1130
["self", "tabsize"], arguments, {tabsize: 8}, null, null)
1131
var s = $B.$GetInt($.tabsize),
1132
col = 0,
1133
pos = 0,
1134
res = "",
1135
chars = to_chars(self)
1136
if(s == 1){
1137
return self.replace(/\t/g," ")
1138
}
1139
while(pos < chars.length){
1140
var car = chars[pos]
1150
res += car
1151
col = 0
1152
break
1153
default:
1154
res += car
1155
col++
1156
break
1157
}
1158
pos++
1159
}
1160
return res
1164
// Return the lowest index in the string where substring sub is found,
1165
// such that sub is contained in the slice s[start:end]. Optional
1166
// arguments start and end are interpreted as in slice notation.
1169
{self: null, sub: null, start: null, end: null},
1170
["self", "sub", "start", "end"],
1171
arguments, {start: 0, end: null}, null, null)
1175
if(!isinstance($.start, _b_.int)||!isinstance($.end, _b_.int)){
1176
throw _b_.TypeError.$factory("slice indices must be " +
1177
"integers or None or have an __index__ method")}
1178
// Can't use string.substring(start, end) because if end < start,
1179
// Javascript transforms it into substring(end, start)...
1180
var s = ""
1181
for(var i = $.start; i < $.end; i++){
1182
s += $.self.charAt(i)
1183
}
1185
var len = str.__len__($.self)
1186
1187
if($.sub.length == 0 && $.start == len){
1188
return len
1189
}
1190
if(s.length + $.sub.length == 0){
1191
return -1
1192
}
1194
var last_search = s.length - $.sub.length
1195
for(var i = 0; i <= last_search; i++){
1196
if(s.substr(i, $.sub.length) == $.sub){
1197
return $.start + str.__len__(s.substr(0, i))
1198
}
1209
// a.x[z]!r:...
1210
// the object has attributes :
1211
// - name : "a"
1212
// - name_ext : [".x", "[z]"]
1213
// - conv : r
1214
// - spec : rest of string after :
1222
// No : in the string : it only contains a name
1223
name = fmt_string
1224
}else{
1225
// name is before the first ":"
1226
// spec (the format specification) is after
1227
name = elts[0]
1231
var elts = name.split("!")
1232
if(elts.length > 1){
1233
name = elts[0]
1234
conv = elts[1] // conversion flag
1238
// "name' may be a subscription or attribute
1239
// Put these "extensions" in the list "name_ext"
1240
function name_repl(match){
1241
name_ext.push(match)
1243
}
1244
var name_ext_re = /\.[_a-zA-Z][_a-zA-Z0-9]*|\[[_a-zA-Z][_a-zA-Z0-9]*\]|\[[0-9]+\]/g
1245
name = name.replace(name_ext_re, name_repl)
1246
}
1253
// Parse self to detect formatting instructions
1254
// Create a list "parts" made of sections of the string :
1255
// - elements of even rank are literal text
1256
// - elements of odd rank are "format objects", built from the
1257
// format strings in self (of the form {...})
1268
text += "{"
1269
pos += 2
1270
}else if(car == "}" && self.charAt(pos + 1) == "}"){
1277
// Store current literal text
1278
parts.push(text)
1279
1280
// Search the end of the format string, ie the } closing the
1281
// opening {. Since the string can contain other pairs {} for
1282
// nested formatting, an integer nb is incremented for each { and
1283
// decremented for each } ; the end of the format string is
1284
// reached when nb == 0
1285
var end = pos + 1,
1286
nb = 1
1287
while(end < _len){
1288
if(self.charAt(end) == "{"){nb++; end++}
1289
else if(self.charAt(end) == "}"){
1290
nb--; end++
1291
if(nb == 0){
1296
var fmt_obj = $B.parse_format(fmt_string)
1297
fmt_obj.raw_name = fmt_obj.name
1298
fmt_obj.raw_spec = fmt_obj.spec
1337
// Special management of keyword arguments if str.format is called by
1338
// str.format_map(mapping) : the argument "mapping" might not be a
1339
// dictionary
1340
var last_arg = $B.last(arguments)
1341
if(last_arg.$nat == "mapping"){
1342
var mapping = last_arg.mapping,
1343
getitem = $B.$getattr(mapping, "__getitem__")
1344
// Get the rest of the arguments
1345
var args = []
1346
for(var i = 0, len = arguments.length - 1; i < len; i++){
1347
args.push(arguments[i])
1348
}
1349
var $ = $B.args("format", 1, {self: null}, ["self"],
1350
args, {}, "$args", null)
1351
}else{
1352
var $ = $B.args("format", 1, {self: null}, ["self"],
1353
arguments, {}, "$args", "$kw"),
1354
mapping = $.$kw, // dictionary
1355
getitem = function(key){
1356
return _b_.dict.$getitem(mapping, key)
1357
}
1358
}
1371
1372
if(fmt.spec !== undefined){
1373
// "spec" may contain "nested replacement fields"
1374
// In this case, evaluate them using the positional
1375
// or keyword arguments passed to format()
1376
function replace_nested(name, key){
1377
if(/\d+/.exec(key)){
1378
// If key is numeric, search in positional
1379
// arguments
1380
return _b_.tuple.__getitem__($.$args,
1381
parseInt(key))
1382
}else{
1383
// Else try in keyword arguments
1384
return _b_.dict.__getitem__($.$kw, key)
1385
}
1386
}
1387
fmt.spec = fmt.spec.replace(/\{(.*?)\}/g,
1388
replace_nested)
1389
}
1391
// Numerical reference : use positional arguments
1392
var pos = parseInt(fmt.name),
1402
// Attribute
1403
value = _b_.getattr(value, ext.substr(1))
1404
}else{
1405
// Subscription
1408
if(key.charAt(0).search(/\d/) > -1){key = parseInt(key)}
1409
value = _b_.getattr(value, "__getitem__")(key)
1413
// If the conversion flag is set, first call a function to convert
1414
// the value
1415
if(fmt.conv == "a"){value = _b_.ascii(value)}
1416
else if(fmt.conv == "r"){value = _b_.repr(value)}
1417
else if(fmt.conv == "s"){value = _b_.str.$factory(value)}
1430
str.format_map = function(self, mapping){
1431
var $ = $B.args("format_map", 2, {self: null, mapping: null},
1432
['self', 'mapping'], arguments, {}, null, null)
1433
return str.format(self, {$nat: 'mapping', mapping})
1446
/* Return true if the string is empty or all characters in the string are
1447
ASCII, false otherwise. ASCII characters have code points in the range
1448
U+0000-U+007F. */
1457
str.isalnum = function(self){
1458
/* Return true if all characters in the string are alphanumeric and there
1459
is at least one character, false otherwise. A character c is alphanumeric
1460
if one of the following returns True: c.isalpha(), c.isdecimal(),
1461
c.isdigit(), or c.isnumeric(). */
1462
var $ = $B.args("isalnum", 1, {self: null}, ["self"],
1463
arguments, {}, null, null),
1464
cp
1465
for(var char of to_chars(self)){
1466
cp = _b_.ord(char)
1467
if(unicode_tables.Ll[cp] ||
1468
unicode_tables.Lu[cp] ||
1469
unicode_tables.Lm[cp] ||
1470
unicode_tables.Lt[cp] ||
1471
unicode_tables.Lo[cp] ||
1472
unicode_tables.Nd[cp] ||
1473
unicode_tables.digits[cp] ||
1474
unicode_tables.numeric[cp]){
1475
continue
1476
}
1477
return false
1478
}
1479
return true
1480
}
1481
1482
str.isalpha = function(self){
1483
/* Return true if all characters in the string are alphabetic and there is
1484
at least one character, false otherwise. Alphabetic characters are those
1485
characters defined in the Unicode character database as "Letter", i.e.,
1486
those with general category property being one of "Lm", "Lt", "Lu", "Ll",
1487
or "Lo". */
1488
var $ = $B.args("isalpha", 1, {self: null}, ["self"],
1489
arguments, {}, null, null),
1490
cp
1491
for(var char of to_chars(self)){
1492
cp = _b_.ord(char)
1493
if(unicode_tables.Ll[cp] ||
1494
unicode_tables.Lu[cp] ||
1495
unicode_tables.Lm[cp] ||
1496
unicode_tables.Lt[cp] ||
1497
unicode_tables.Lo[cp]){
1498
continue
1499
}
1500
return false
1501
}
1502
return true
1503
}
1504
1505
str.isdecimal = function(self){
1506
/* Return true if all characters in the string are decimal characters and
1507
there is at least one character, false otherwise. Decimal characters are
1508
those that can be used to form numbers in base 10, e.g. U+0660,
1509
ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in
1510
the Unicode General Category "Nd". */
1511
var $ = $B.args("isdecimal", 1, {self: null}, ["self"],
1512
arguments, {}, null, null),
1513
cp
1514
for(var char of to_chars(self)){
1515
cp = _b_.ord(char)
1516
if(! unicode_tables.Nd[cp]){
1517
return false
1518
}
1519
}
1520
return self.length > 0
1521
}
1522
1523
str.isdigit = function(self){
1524
/* Return true if all characters in the string are digits and there is at
1525
least one character, false otherwise. */
1526
var $ = $B.args("isdigit", 1, {self: null}, ["self"],
1527
arguments, {}, null, null),
1528
cp
1529
for(var char of to_chars(self)){
1530
cp = _b_.ord(char)
1531
if(! unicode_tables.digits[cp]){
1532
return false
1533
}
1534
}
1535
return self.length > 0
1536
}
1537
1538
str.isidentifier = function(self){
1539
/* Return true if the string is a valid identifier according to the
1540
language definition. */
1541
var $ = $B.args("isidentifier", 1, {self: null}, ["self"],
1545
}
1546
var chars = to_chars(self)
1547
if(unicode_tables.XID_Start[_b_.ord(chars[0])] === undefined){
1550
for(var char of chars){
1551
var cp = _b_.ord(char)
1552
if(unicode_tables.XID_Continue[cp] === undefined){
1553
return false
1554
}
1555
}
1556
}
1557
return true
1558
}
1559
1560
str.islower = function(self){
1561
/* Return true if all cased characters 4 in the string are lowercase and
1562
there is at least one cased character, false otherwise. */
1563
var $ = $B.args("islower", 1, {self: null}, ["self"],
1564
arguments, {}, null, null),
1565
has_cased = false,
1568
for(var char of to_chars(self)){
1569
cp = _b_.ord(char)
1570
if(unicode_tables.Ll[cp]){
1571
has_cased = true
1572
continue
1573
}else if(unicode_tables.Lu[cp] || unicode_tables.Lt[cp]){
1574
return false
1575
}
1576
}
1577
return has_cased
1578
}
1579
1580
str.isnumeric = function(self){
1581
/* Return true if all characters in the string are numeric characters, and
1582
there is at least one character, false otherwise. Numeric characters
1583
include digit characters, and all characters that have the Unicode numeric
1584
value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric
1585
characters are those with the property value Numeric_Type=Digit,
1586
Numeric_Type=Decimal or Numeric_Type=Numeric.*/
1587
var $ = $B.args("isnumeric", 1, {self: null}, ["self"],
1588
arguments, {}, null, null)
1597
var unprintable = {},
1598
unprintable_gc = ['Cc', 'Cf', 'Co', 'Cs','Zl', 'Zp', 'Zs']
1599
1600
str.isprintable = function(self){
1601
/* Return true if all characters in the string are printable or the string
1602
is empty, false otherwise. Nonprintable characters are those characters
1603
defined in the Unicode character database as "Other" or "Separator",
1604
excepting the ASCII space (0x20) which is considered printable. */
1605
1606
// Set unprintable if not set yet
1607
if(Object.keys(unprintable).length == 0){
1608
for(var i = 0; i < unprintable_gc.length; i++){
1609
var table = unicode_tables[unprintable_gc[i]]
1620
return false
1621
}
1622
}
1623
return true
1624
}
1625
1626
str.isspace = function(self){
1627
/* Return true if there are only whitespace characters in the string and
1628
there is at least one character, false otherwise.
1629
1630
A character is whitespace if in the Unicode character database, either its
1631
general category is Zs ("Separator, space"), or its bidirectional class is
1632
one of WS, B, or S.*/
1633
var $ = $B.args("isspace", 1, {self: null}, ["self"],
1634
arguments, {}, null, null),
1635
cp
1636
for(var char of to_chars(self)){
1637
cp = _b_.ord(char)
1638
if(! unicode_tables.Zs[cp] &&
1639
$B.unicode_bidi_whitespace.indexOf(cp) == -1){
1640
return false
1641
}
1642
}
1643
return self.length > 0
1644
}
1645
1646
str.istitle = function(self){
1647
/* Return true if the string is a titlecased string and there is at least
1648
one character, for example uppercase characters may only follow uncased
1649
characters and lowercase characters only cased ones. Return false
1650
otherwise. */
1651
var $ = $B.args("istitle", 1, {self: null}, ["self"],
1652
arguments, {}, null, null)
1653
return self.length > 0 && str.title(self) == self
1654
}
1655
1656
str.isupper = function(self){
1657
/* Return true if all cased characters 4 in the string are lowercase and
1658
there is at least one cased character, false otherwise. */
1659
var $ = $B.args("islower", 1, {self: null}, ["self"],
1660
arguments, {}, null, null),
1664
for(var char of to_chars(self)){
1665
cp = _b_.ord(char)
1666
if(unicode_tables.Lu[cp]){
1667
is_upper = true
1668
continue
1669
}else if(unicode_tables.Ll[cp] || unicode_tables.Lt[cp]){
1687
if(! isinstance(obj2, str)){
1688
throw _b_.TypeError.$factory("sequence item " + count +
1689
": expected str instance, " + $B.class_name(obj2) +
1690
" found")
1691
}
1704
var $ = $B.args("ljust", 3, {self: null, width: null, fillchar:null},
1705
["self", "width", "fillchar"],
1709
if($.width <= len){
1710
return self
1711
}
1712
return self + $.fillchar.repeat($.width - len)
1715
str.lower = function(self){
1716
var $ = $B.args("lower", 1, {self: null}, ["self"],
1717
arguments, {}, null, null)
1718
return self.toLowerCase()
1719
}
1720
1722
var $ = $B.args("lstrip", 2, {self: null, chars: null}, ["self", "chars"],
1723
arguments, {chars:_b_.None}, null, null)
1724
if($.chars === _b_.None){
1725
return $.self.trimLeft()
1726
}
1727
var chars = to_chars(self)
1728
for(var i = 0, len = chars.length; i < len; i++){
1729
if($.chars.indexOf(chars[i]) === -1){
1730
return chars.slice(i).join('')
1738
var $ = $B.args("maketrans", 3, {x: null, y: null, z: null},
1739
["x", "y", "z"], arguments, {y: null, z: null}, null, null)
1744
// If there is only one argument, it must be a dictionary mapping
1745
// Unicode ordinals (integers) or characters (strings of length 1) to
1746
// Unicode ordinals, strings (of arbitrary lengths) or None. Character
1748
if(! _b_.isinstance($.x, _b_.dict)){
1749
throw _b_.TypeError.$factory(
1750
"maketrans only argument must be a dict")
1753
for(var i = 0, len = items.length; i < len; i++){
1754
var k = items[i][0],
1755
v = items[i][1]
1756
if(! _b_.isinstance(k, _b_.int)){
1757
if(_b_.isinstance(k, _b_.str) && k.length == 1){
1758
k = _b_.ord(k)
1759
}else{throw _b_.TypeError.$factory("dictionary key " + k +
1762
if(v !== _b_.None && ! _b_.isinstance(v, [_b_.int, _b_.str])){
1763
throw _b_.TypeError.$factory("dictionary value " + v +
1771
// and in the resulting dictionary, each character in x will be mapped
1772
// to the character at the same position in y
1775
}else if($.x.length !== $.y.length){
1776
throw _b_.TypeError.$factory(
1777
"maketrans arguments must be strings or same length")
1783
if(! _b_.isinstance($.z, _b_.str)){
1784
throw _b_.TypeError.$factory(
1785
"maketrans third argument must be a string")
1807
var $ = $B.args("partition", 2, {self: null, sep: null}, ["self", "sep"],
1808
arguments, {}, null, null)
1813
var chars = to_chars(self),
1814
i = chars.indexOf($.sep)
1815
if(i == -1){
1816
return _b_.tuple.$factory([$.self, "", ""])
1817
}
1818
return _b_.tuple.$factory([chars.slice(0, i).join(''), $.sep,
1819
chars.slice(i + $.sep.length).join('')])
1822
str.removeprefix = function(){
1823
var $ = $B.args("removeprefix", 2, {self: null, prefix: null},
1824
["self", "prefix"], arguments, {}, null, null)
1825
if(!_b_.isinstance($.prefix, str)){
1826
throw _b_.ValueError.$factory("prefix should be str, not " +
1827
`'${$B.class_name($.prefix)}'`)
1828
}
1829
if(str.startswith($.self, $.prefix)){
1830
return $.self.substr($.prefix.length)
1831
}
1832
return $.self.substr(0)
1833
}
1834
1835
str.removesuffix = function(){
1836
var $ = $B.args("removesuffix", 2, {self: null, prefix: null},
1837
["self", "suffix"], arguments, {}, null, null)
1838
if(!_b_.isinstance($.suffix, str)){
1839
throw _b_.ValueError.$factory("suffix should be str, not " +
1840
`'${$B.class_name($.prefix)}'`)
1841
}
1842
if($.suffix.length > 0 && str.endswith($.self, $.suffix)){
1843
return $.self.substr(0, $.self.length - $.suffix.length)
1844
}
1845
return $.self.substr(0)
1846
}
1847
1848
function $re_escape(str){
1849
var specials = "[.*+?|()$^"
1850
for(var i = 0, len = specials.length; i < len; i++){
1851
var re = new RegExp("\\"+specials.charAt(i), "g")
1852
str = str.replace(re, "\\"+specials.charAt(i))
1853
}
1854
return str
1861
var $ = $B.args("replace", 4,
1862
{self: null, old: null, $$new: null, count: null},
1863
["self", "old", "$$new", "count"],
1864
arguments, {count: -1}, null, null),
1865
count = $.count,
1866
self = $.self,
1867
old = $.old,
1868
_new = $.$$new
1875
"' object cannot be interpreted as an integer")
1876
}else if(isinstance(count, _b_.float)){
1877
throw _b_.TypeError.$factory("integer argument expected, got float")
1878
}
1879
if(count == 0){
1880
return self
1881
}
1882
if(count.__class__ == $B.long_int){
1883
count = parseInt(count.value)
1884
}
1886
if(_new == ""){
1887
return self
1888
}
1889
if(self == ""){
1890
return _new
1891
}
1892
var elts = self.split("")
1893
if(count > -1 && elts.length >= count){
1894
var rest = elts.slice(count).join("")
1895
return _new + elts.slice(0, count).join(_new) + rest
1921
res = res.substr(0, pos) + _new + res.substr(pos + old.length)
1922
pos = pos + _new.length
1923
count--
1929
// Return the highest index in the string where substring sub is found,
1930
// such that sub is contained within s[start:end]. Optional arguments
1932
if(arguments.length == 2 && typeof substr == "string"){
1933
return self.lastIndexOf(substr)
1934
}
1936
{self: null, sub: null, start: null, end: null},
1937
["self", "sub", "start", "end"],
1938
arguments, {start: 0, end: null}, null, null)
1945
if($.start > $.self.length){
1946
return -1
1947
}else{
1948
return str.__len__($.self)
1949
}
1955
if($.self.substr(i, sublen) == $.sub){
1956
return str.__len__($.self.substr(0, i))
1957
}
1972
var $ = $B.args("rjust",3,
1973
{self: null, width: null, fillchar: null},
1974
["self", "width", "fillchar"],
1975
arguments, {fillchar: " "}, null, null)
1985
var $ = $B.args("rpartition", 2, {self: null, sep: null}, ["self", "sep"],
1986
arguments, {}, null, null)
1990
var items = str.partition(self, sep).reverse()
1991
for(var i = 0; i < items.length; i++){
1992
items[i] = items[i].split("").reverse().join("")
1998
var $ = $B.args("rsplit", 3, {self: null, sep: null, maxsplit: null},
1999
["self", "sep", "maxsplit"], arguments,
2000
{sep: _b_.None, maxsplit: -1}, null, null),
2001
sep = $.sep
2004
var rev_str = reverse($.self),
2005
rev_sep = sep === _b_.None ? sep : reverse($.sep),
2016
str.rstrip = function(self, x){
2017
var $ = $B.args("rstrip", 2, {self: null, chars: null}, ["self", "chars"],
2019
if($.chars === _b_.None){
2020
return $.self.trimRight()
2021
}
2022
var chars = to_chars(self)
2023
for(var j = chars.length - 1; j >= 0; j--){
2024
if($.chars.indexOf(chars[j]) == -1){
2025
return chars.slice(0, j + 1).join('')
2032
var $ = $B.args("split", 3, {self: null, sep: null, maxsplit: null},
2033
["self", "sep", "maxsplit"], arguments,
2034
{sep: _b_.None, maxsplit: -1}, null, null),
2035
sep = $.sep,
2036
maxsplit = $.maxsplit,
2037
self = $.self,
2038
pos = 0
2039
if(maxsplit.__class__ === $B.long_int){
2040
maxsplit = parseInt(maxsplit.value)
2041
}
2042
if(sep == ""){
2043
throw _b_.ValueError.$factory("empty separator")
2044
}
2047
while(pos < self.length && self.charAt(pos).search(/\s/) > -1){
2048
pos++
2049
}
2050
if(pos === self.length - 1){
2051
return [self]
2052
}
2056
if(name == ""){
2057
name = self.charAt(pos)
2058
}else{
2059
name += self.charAt(pos)
2060
}
2082
var res = [],
2083
s = "",
2084
seplen = sep.length
2085
if(maxsplit == 0){return [self]}
2086
while(pos < self.length){
2087
if(self.substr(pos, seplen) == sep){
2105
str.splitlines = function(self) {
2106
var $ = $B.args('splitlines', 2, {self: null, keepends: null},
2107
['self','keepends'], arguments, {keepends: false},
2108
null, null)
2109
if(!_b_.isinstance($.keepends,[_b_.bool, _b_.int])){
2110
throw _b_.TypeError('integer argument expected, got '+
2113
var keepends = _b_.int.$factory($.keepends),
2114
res = [],
2115
self = $.self,
2116
start = 0,
2117
pos = 0
2118
if(!self.length){
2122
if(self.substr(pos, 2) == '\r\n'){
2123
res.push(self.slice(start, keepends ? pos + 2 : pos))
2124
start = pos = pos+2
2125
}else if(self[pos] == '\r' || self[pos] == '\n'){
2126
res.push(self.slice(start, keepends ? pos+1 : pos))
2127
start = pos = pos+1
2128
}else{
2129
pos++
2130
}
2131
}
2132
if(start < self.length){
2133
res.push(self.slice(start))
2134
}
2135
return res
2139
// Return True if string starts with the prefix, otherwise return False.
2140
// prefix can also be a tuple of prefixes to look for. With optional
2141
// start, test string beginning at that position. With optional end,
2143
var $ = $B.args("startswith", 4,
2144
{self: null, prefix: null, start: null, end: null},
2145
["self", "prefix", "start", "end"],
2146
arguments, {start: 0, end: null}, null, null)
2158
if(! _b_.isinstance(prefix, str)){
2159
throw _b_.TypeError.$factory("endswith first arg must be str " +
2160
"or a tuple of str, not int")
2161
}
2162
if(s.substr(0, prefix.length) == prefix){
2163
return true
2164
}
2170
var $ = $B.args("strip", 2, {self: null, chars: null}, ["self", "chars"],
2171
arguments, {chars: _b_.None}, null, null)
2172
if($.chars === _b_.None){
2173
return $.self.trim()
2174
}
2175
var chars = to_chars($.self)
2176
for(var i = 0; i < chars.length; i++){
2177
if($.chars.indexOf(chars[i]) == -1){
2181
for(var j = chars.length - 1; j >= i; j--){
2182
if($.chars.indexOf(chars[j]) == -1){
2189
str.swapcase = function(self){
2190
var $ = $B.args("swapcase", 1, {self}, ["self"],
2191
arguments, {}, null, null),
2192
res = "",
2193
cp
2194
2195
for(var char of to_chars(self)){
2196
cp = _b_.ord(char)
2197
if(unicode_tables.Ll[cp]){
2198
res += char.toUpperCase()
2199
}else if(unicode_tables.Lu[cp]){
2200
res += char.toLowerCase()
2203
}
2204
}
2205
return res
2206
}
2207
2208
str.title = function(self){
2209
var $ = $B.args("title", 1, {self}, ["self"],
2210
arguments, {}, null, null),
2211
state,
2214
for(var char of to_chars(self)){
2215
cp = _b_.ord(char)
2216
if(unicode_tables.Ll[cp]){
2223
}else if(unicode_tables.Lu[cp] || unicode_tables.Lt[cp]){
2224
res += state ? char.toLowerCase() : char
2236
getitem = $B.$getattr(table, "__getitem__"),
2237
cp
2238
for(var char of to_chars(self)){
2239
cp = _b_.ord(char)
2243
if(typeof repl == "string"){
2244
res.push(repl)
2245
}else if(typeof repl == "number"){
2246
res.push(String.fromCharCode(repl))
2247
}
2256
str.upper = function(self){
2257
var $ = $B.args("upper", 1, {self: null}, ["self"],
2258
arguments, {}, null, null)
2259
return self.toUpperCase()
2260
}
2261
2264
["self", "width"], arguments, {}, null, null),
2265
len = str.__len__(self)
2266
if($.width <= len){
2267
return self
2268
}
2288
if(encoding !== undefined){
2289
// Arguments may be passed as keywords (cf. issue #1060)
2290
var $ = $B.args("str", 3, {arg: null, encoding: null, errors: null},
2291
["arg", "encoding", "errors"], arguments,
2292
{encoding: "utf-8", errors: "strict"}, null, null),
2293
encoding = $.encoding,
2294
errors = $.errors
2295
}
2306
// class or its subclasses, but the attribute __str__ of the
2307
// class metaclass (usually "type") or its subclasses (usually
2308
// "object")
2309
// The metaclass is the attribute __class__ of the class dictionary
2314
if(arg.__class__ && arg.__class__ === _b_.bytes &&
2315
encoding !== undefined){
2316
// str(bytes, encoding, errors) is equal to
2317
// bytes.decode(encoding, errors)
2320
// Implicit invocation of __str__ uses method __str__ on the class,
2321
// even if arg has an attribute __str__
2322
var klass = arg.__class__ || $B.get_class(arg)
2328
// if not better than object.__str__, try __repr__
2329
(arg.__class__ && arg.__class__ !== _b_.object &&
2330
method.$infos && method.$infos.__func__ === _b_.object.__str__)){
2331
var method = $B.$getattr(klass, "__repr__")
2337
if($B.debug > 1){console.log(err)}
2338
console.log("Warning - no method __str__ or __repr__, " +
2339
"default to toString", arg)
2346
if(cls === undefined){
2347
throw _b_.TypeError.$factory("str.__new__(): not enough arguments")
2371
var args = [],
2372
pos = 0
2373
if(arguments.length > 0){
2374
var args = [arguments[0].valueOf()],
2375
pos = 1
2376
for(var i = 1, len = arguments.length; i < len; i++){
2377
args[pos++] = arguments[i]
2394
// Function to parse the 2nd argument of format()
2395
$B.parse_format_spec = function(spec){
2399
var pos = 0,
2400
aligns = "<>=^",
2401
digits = "0123456789",
2402
types = "bcdeEfFgGnosxX%",
2404
if(align_pos != -1){
2405
if(spec.charAt(1) && aligns.indexOf(spec.charAt(1)) != -1){
2406
// If the second char is also an alignment specifier, the
2407
// first char is the fill value
2408
this.fill = spec.charAt(0)
2409
this.align = spec.charAt(1)
2410
pos = 2
2411
}else{
2412
// The first character defines alignment : fill defaults to ' '
2427
if(car == "+" || car == "-" || car == " "){
2428
this.sign = car
2429
pos++
2430
car = spec.charAt(pos)
2445
if(this.width === undefined){
2446
this.width = car
2447
}else{
2448
this.width += car
2449
}
2456
if(this.width === undefined && car == "{"){
2457
// Width is determined by a parameter
2458
var end_param_pos = spec.substr(pos).search("}")
2459
this.width = spec.substring(pos, end_param_pos)
2460
console.log("width", "[" + this.width + "]")
2461
pos += end_param_pos + 1
2462
}
2463
if(car == ","){
2464
this.comma = true
2465
pos++
2466
car = spec.charAt(pos)
2467
}
2468
if(car == "."){
2469
if(digits.indexOf(spec.charAt(pos + 1)) == -1){
2470
throw _b_.ValueError.$factory(
2471
"Missing precision in format spec")
2473
this.precision = spec.charAt(pos + 1)
2474
pos += 2
2475
car = spec.charAt(pos)
2476
while(car && digits.indexOf(car) > -1){
2483
if(car && types.indexOf(car) > -1){
2484
this.type = car
2485
pos++
2486
car = spec.charAt(pos)
2487
}
2488
if(pos !== spec.length){
2494
return (this.fill === undefined ? "" : _b_.str.$factory(this.fill)) +
2495
(this.align || "") +
2496
(this.sign || "") +
2497
(this.alternate ? "#" : "") +
2498
(this.sign_aware ? "0" : "") +
2499
(this.width || "") +
2500
(this.comma ? "," : "") +
2501
(this.precision ? "." + this.precision : "") +
2502
(this.type || "")
2507
if(fmt.width && s.length < fmt.width){
2508
var fill = fmt.fill || " ",
2509
align = fmt.align || "<",
2510
missing = fmt.width - s.length
2512
case "<":
2513
return s + fill.repeat(missing)
2514
case ">":
2515
return fill.repeat(missing) + s
2516
case "=":
2517
if("+-".indexOf(s.charAt(0)) > -1){
2518
return s.charAt(0) + fill.repeat(missing) + s.substr(1)
2522
case "^":
2523
var left = parseInt(missing / 2)
2524
return fill.repeat(left) + s + fill.repeat(missing - left)
2537
function fstring_error(msg, pos){
2538
error = Error(msg)
2539
error.position = pos
2540
throw error
2541
}
2542
2543
$B.parse_fstring = function(string){
2544
// Parse a f-string
2545
var elts = [],
2546
pos = 0,
2608
}else if(ctype == "debug"){
2609
// after the equal sign, whitespace are ignored and the only
2610
// valid characters are } and :
2611
while(string.charAt(i) == " "){i++}
2612
if(string.charAt(i) == "}"){
2613
// end of debug expression
2614
elts.push(current)
2615
ctype = null
2616
current = ""
2617
pos = i + 1
2618
}
2619
}else{
2620
// End of expression is the } matching the opening {
2621
// There may be nested braces
2622
var i = pos,
2623
nb_braces = 1,
2636
if(current.expression == ""){
2637
fstring_error("f-string: empty expression not allowed",
2638
pos)
2639
}
2649
// backslash is not allowed in expressions
2650
throw Error("f-string expression part cannot include a" +
2651
" backslash")
2658
throw Error("f-string: invalid conversion character:" +
2659
" expected 's', 'r', or 'a'")
2660
}else{
2674
if(string.substr(i, 3) == '"""'){
2675
var end = string.indexOf('"""', i + 3)
2676
if(end == -1){
2678
}else{
2679
var trs = string.substring(i, end + 3)
2680
trs = trs.replace("\n", "\\n\\")
2699
var ce = current.expression,
2700
last_char = ce.charAt(ce.length - 1),
2701
last_char_re = ('()'.indexOf(last_char) > -1 ? "\\" : "") + last_char
2706
"=!<>:".search(last_char_re) > -1){
2707
current.expression += car //+ string.charAt(i + 1)
2708
i += 1
2709
}else{
2710
// add debug string
2711
tail = car
2712
while(string.charAt(i + 1).match(/\s/)){
2713
tail += string.charAt(i + 1)
2714
i++
2715
}
2716
elts.push(current.expression + tail)
2717
// remove trailing whitespace from expression
2718
while(ce.match(/\s$/)){
2719
ce = ce.substr(0, ce.length - 1)
2720
}
2721
current.expression = ce
2722
ctype = "debug"
2723
i++
2724
}
2740
if(i >= 0x10000 && i <= 0x10FFFF){
2741
var code = (i - 0x10000)
2742
return String.fromCodePoint(0xD800 | (code >> 10)) +
2743
String.fromCodePoint(0xDC00 | (code & 0x3FF))
2744
}else{
2745
return String.fromCodePoint(i)
2746
}
2747
}
2750
if(c.length == 1){
2751
return c.charCodeAt(0)
2752
}
2753
var code = 0x10000
2754
code += (c.charCodeAt(0) & 0x03FF) << 10
2755
code += (c.charCodeAt(1) & 0x03FF)
2756
return code
2757
}
2758