Permalink
Mar 6, 2021
Aug 7, 2021
Mar 19, 2018
Apr 5, 2021
Mar 6, 2021
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
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
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 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
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
Oct 13, 2019
Oct 13, 2019
Jul 31, 2021
Jan 18, 2021
Jan 18, 2021
Newer
100644
2893 lines (2679 sloc)
87.2 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
24
for(var i = 0, len = s.length; i < len; i++){
25
var cp = s.codePointAt(i)
26
if(cp >= 0x10000){
35
var res = new String(s)
36
res.__class__ = str
37
res.surrogates = surrogates
38
return res
39
}
40
41
function pypos2jspos(s, pypos){
42
// convert Python position to JS position
43
if(s.surrogates === undefined){
44
return pypos
45
}
46
var nb = 0
47
while(s.surrogates[nb] < pypos){
48
nb++
49
}
50
return pypos + nb
65
if(typeof $.self == "string"){
66
$.self = $B.String($.self)
67
}
68
var len = str.__len__($.self)
69
if($.start === null || $.start === _b_.None){
70
$.start = 0
71
}else if($.start < 0){
72
$.start += len
75
if($.end === null || $.end === _b_.None){
76
$.end = len
77
}else if($.end < 0){
78
$.end += len
83
throw _b_.TypeError.$factory("slice indices must be integers " +
84
"or None or have an __index__ method")
85
}
97
throw _b_.TypeError.$factory((prefix || '') +
98
"must be str, not " + $B.class_name(obj))
102
function to_chars(s){
103
// Transform Javascript string s into a list of Python characters
104
// (2 JS chars if surrogate, 1 otherwise)
105
var chars = []
106
for(var i = 0, len = s.length; i < len; i++){
107
var code = s.charCodeAt(i)
108
if(code >= 0xD800 && code <= 0xDBFF){
109
chars.push(s.substr(i, 2))
110
i++
111
}else{
112
chars.push(s.charAt(i))
113
}
114
}
115
return chars
116
}
117
118
function to_codepoints(s){
119
// Transform Javascript string s into a list of codepoints
123
var cps = []
124
for(var i = 0, len = s.length; i < len; i++){
125
var code = s.charCodeAt(i)
126
if(code >= 0xD800 && code <= 0xDBFF){
127
var v = 0x10000
128
v += (code & 0x03FF) << 10
129
v += (s.charCodeAt(i + 1) & 0x03FF)
130
cps.push(v)
131
i++
132
}else{
133
cps.push(code)
134
}
135
}
140
if(! _b_.isinstance(other, str)){
141
try{
142
return $B.$getattr(other, "__radd__")(self)
143
}catch(err){
152
throw _b_.TypeError.$factory("'in <string>' requires " +
153
"string as left operand, not " + item.__class__)
154
}
180
// __dir__must be assigned explicitely because attribute resolution for
181
// builtin classes doesn't use __mro__
193
if(fmt.type && fmt.type != "s"){
194
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
202
if(fmt.sign !== undefined){
203
throw _b_.ValueError.$factory(
204
"Sign not allowed in string format specifier")
221
if(pos >= 0 && pos < len){
222
var jspos = pypos2jspos(self, pos)
223
if(self.codePointAt(jspos) >= 0x10000){
224
return $B.String(self.substr(jspos, 2))
225
}else{
226
return $B.String(self[jspos])
227
}
232
var s = _b_.slice.$conv_for_seq(arg, len),
233
start = pypos2jspos(self, s.start),
234
stop = pypos2jspos(self, s.stop),
297
try{
298
return str_hash_cache[self] = fnv(to_codepoints(self))
299
}catch(err){
300
console.log('error hash, cps', self, to_codepoints(self))
301
throw err
302
}
320
if(self.len !== undefined){
321
return self.len
322
}
323
var len = self.len = self.valueOf().length - self.surrogates.length
324
return len
361
// left adjusted
362
return s + get_char_array(padding - s.length, flags.pad_char)
363
}
364
}
365
373
if(val.__class__ === $B.long_int){
374
s = $B.long_int.to_base(val, 10)
375
}else{
376
s = val.toString()
378
if(s[0] === "-"){
379
return "-" + get_char_array(precision - s.length + 1, "0") + s.slice(1)
390
if(val === Infinity){
391
val = "inf"
392
}else if(val === -Infinity){
393
val = "-inf"
394
}else{
395
val = "nan"
417
var str_format = function(val, flags) {
418
// string format supports left and right padding
419
flags.pad_char = " " // even if 0 padding is defined, don't use it
425
if(val.__class__ === $B.long_int){
426
val = $B.long_int.to_base(val, 10)
427
}else{
428
val = parseInt(val)
446
var repr_format = function(val, flags) {
447
flags.pad_char = " " // even if 0 padding is defined, don't use it
451
var ascii_format = function(val, flags) {
452
flags.pad_char = " " // even if 0 padding is defined, don't use it
466
flags.precision = parseInt(flags.precision, 10)
467
validate_precision(flags.precision)
468
}
469
return parseFloat(val)
470
}
473
var trailing_zeros = /(.*?)(0+)([eE].*)/,
474
leading_zeros = /\.(0*)/,
475
trailing_dot = /\.$/
477
var validate_precision = function(precision) {
478
// force precision to limits of javascript
483
var floating_point_format = function(val, upper, flags){
484
val = _float_helper(val, flags),
485
v = val.toString(),
486
v_len = v.length,
487
dot_idx = v.indexOf('.')
488
if(dot_idx < 0){dot_idx = v_len}
489
if(val < 1 && val > -1){
490
var zeros = leading_zeros.exec(v),
491
numzeros
492
if(zeros){
497
if(numzeros >= 4){
498
val = format_sign(val, flags) + format_float_precision(val, upper,
499
flags, _floating_g_exp_helper)
500
if(!flags.alternate){
513
return format_padding(format_sign(val, flags) +
514
format_float_precision(val, upper, flags,
515
function(val, precision) {
517
numzeros)
518
}),
519
flags
520
)
521
}
522
523
if(dot_idx > flags.precision){
524
val = format_sign(val, flags) + format_float_precision(val, upper,
525
flags, _floating_g_exp_helper)
526
if(! flags.alternate){
538
return format_padding(format_sign(val, flags) +
539
format_float_precision(val, upper, flags,
540
function(val, precision) {
541
if(!flags.decimal_point){
543
}else if (precision > v_len){
544
if(! flags.alternate){
545
precision = v_len
546
}
548
if(precision < dot_idx){
549
precision = dot_idx
550
}
551
return val.toFixed(precision - dot_idx)
552
}),
553
flags
554
)
557
var _floating_g_exp_helper = function(val, precision, flags, upper){
558
if(precision){--precision}
561
var e_idx = val.lastIndexOf("e")
562
if(e_idx > val.length - 4){
563
val = val.substring(0, e_idx + 2) + "0" + val.substring(e_idx + 2)
566
return val
567
}
568
569
// fF
570
var floating_point_decimal_format = function(val, upper, flags) {
571
val = _float_helper(val, flags)
572
return format_padding(format_sign(val, flags) +
573
format_float_precision(val, upper, flags,
574
function(val, precision, flags) {
575
val = val.toFixed(precision)
576
if(precision === 0 && flags.alternate){
577
val += '.'
578
}
579
return val
580
}),
581
flags
582
)
583
}
584
585
var _floating_exp_helper = function(val, precision, flags, upper) {
586
val = val.toExponential(precision)
587
// pad exponent to two digits
600
return format_padding(format_sign(val, flags) +
601
format_float_precision(val, upper, flags, _floating_exp_helper), flags)
627
if(flags.alternate){
628
if(ret.charAt(0) === "-"){
629
if(upper){ret = "-0X" + ret.slice(1)}
630
else{ret = "-0x" + ret.slice(1)}
631
}else{
632
if(upper){ret = "0X" + ret}
633
else{ret = "0x" + ret}
643
if(val.__class__ === $B.long_int){
644
ret = $B.long_int.to_base(8)
645
}else{
646
ret = parseInt(val)
647
ret = ret.toString(8)
663
if(flags.alternate){
664
if(ret.charAt(0) === "-"){ret = "-0o" + ret.slice(1)}
665
else{ret = "0o" + ret}
670
function series_of_bytes(val, flags){
671
if(val.__class__ && val.__class__.$buffer_protocol){
672
var it = _b_.iter(val),
673
ints = []
674
while(true){
675
try{
676
ints.push(_b_.next(it))
677
}catch(err){
678
if(err.__class__ === _b_.StopIteration){
679
var b = _b_.bytes.$factory(ints)
680
return format_padding(_b_.bytes.decode(b, "ascii"), flags)
681
}
682
throw err
683
}
684
}
685
}else{
686
try{
687
bytes_obj = $B.$getattr(val, "__bytes__")
688
return format_padding(_b_.bytes.decode(bytes_obj), flags)
689
}catch(err){
690
if(err.__class__ === _b_.AttributeError){
691
throw _b_.TypeError.$factory("%b does not accept '" +
692
$B.class_name(val) + "'")
693
}
694
throw err
695
}
696
}
697
}
698
703
val = val.source[0]
704
}else{
705
try{
706
val = _b_.int.$factory(val) // yes, floats are valid (they are cast to int)
707
}catch (err){
708
throw _b_.TypeError.$factory("%c requires int or char")
709
}
714
var num_flag = function(c, flags){
715
if(c === "0" && ! flags.padding && ! flags.decimal_point && ! flags.left){
716
flags.pad_char = "0"
722
flags.precision = (flags.precision || "") + c
723
}
724
}
725
726
var decimal_point_flag = function(val, flags) {
728
// can only have one decimal point
729
throw new UnsupportedChar()
730
}
731
flags.decimal_point = true
732
}
733
734
var neg_flag = function(val, flags){
735
flags.pad_char = " " // overrides '0' flag
753
"s": str_format,
754
"d": num_format,
755
"i": num_format,
756
"u": num_format,
757
"o": octal_format,
758
"r": repr_format,
759
"a": ascii_format,
760
"g": function(val, flags){
761
return floating_point_format(val, false, flags)
762
},
763
"G": function(val, flags){return floating_point_format(val, true, flags)},
764
"f": function(val, flags){
765
return floating_point_decimal_format(val, false, flags)
766
},
767
"F": function(val, flags){
768
return floating_point_decimal_format(val, true, flags)
769
},
770
"e": function(val, flags){
771
return floating_point_exponential_format(val, false, flags)
772
},
773
"E": function(val, flags){
774
return floating_point_exponential_format(val, true, flags)
775
},
776
"x": function(val, flags){return signed_hex_format(val, false, flags)},
777
"X": function(val, flags){return signed_hex_format(val, true, flags)},
778
"c": single_char_format,
779
"0": function(val, flags){return num_flag("0", flags)},
780
"1": function(val, flags){return num_flag("1", flags)},
781
"2": function(val, flags){return num_flag("2", flags)},
782
"3": function(val, flags){return num_flag("3", flags)},
783
"4": function(val, flags){return num_flag("4", flags)},
784
"5": function(val, flags){return num_flag("5", flags)},
785
"6": function(val, flags){return num_flag("6", flags)},
786
"7": function(val, flags){return num_flag("7", flags)},
787
"8": function(val, flags){return num_flag("8", flags)},
788
"9": function(val, flags){return num_flag("9", flags)},
789
"-": neg_flag,
790
" ": space_flag,
791
"+": sign_flag,
792
".": decimal_point_flag,
793
"#": alternate_flag
794
}
795
796
// exception thrown when an unsupported char is encountered in legacy format
842
if(self === undefined){
843
throw _b_.TypeError.$factory(
844
"not enough arguments for format string")
871
throw _b_.ValueError.$factory(
872
"unsupported format character '" + invalid_char +
873
"' (0x" + invalid_char.charCodeAt(0).toString(16) +
874
") at index " + newpos)
875
}else if(err.name === "NotANumber"){
876
var try_char = s[newpos],
877
cls = self.__class__
878
if(!cls){
879
if(typeof(self) === "string"){
880
cls = "str"
881
}else{
887
throw _b_.TypeError.$factory("%" + try_char +
888
" format: a number is required, not " + cls)
889
}else{
921
}while(pos < length)
922
923
if(argpos !== null){
924
if(args.length > argpos){
925
throw _b_.TypeError.$factory(
926
"not enough arguments for format string")
927
}else if(args.length < argpos){
928
throw _b_.TypeError.$factory(
929
"not all arguments converted during string formatting")
931
}else if(nbph == 0){
932
throw _b_.TypeError.$factory(
933
"not all arguments converted during string formatting")
941
var $ = $B.args("__mul__", 2, {self: null, other: null},
942
["self", "other"], arguments, {}, null, null)
953
function __newobj__(){
954
// __newobj__ is called with a generator as only argument
955
var $ = $B.args('__newobj__', 0, {}, [], arguments, {}, 'args', null),
956
args = $.args
957
var res = args[1]
958
res.__class__ = args[0]
959
return res
960
}
961
962
str.__reduce_ex__ = function(self){
963
return $B.fast_tuple([
964
__newobj__,
965
$B.fast_tuple([self.__class__ || _b_.str, self]),
966
_b_.None,
967
_b_.None])
968
}
969
972
var t = {
973
8: "\\x08",
974
9: "\\t",
975
10: "\\n",
976
11: "\\x0b",
977
12: "\\x0c",
978
13: "\\r",
979
92: "\\\\"
980
}
981
var repl = '',
982
chars = to_chars(self)
983
for(var i = 0; i < chars.length; i++){
984
var cp = _b_.ord(chars[i])
985
if(t[cp] !== undefined){
986
repl += t[cp]
987
}else if($B.is_unicode_cn(cp)){
1006
if(res.search('"') == -1 && res.search("'") == -1){
1007
return "'" + res + "'"
1008
}else if(self.search('"') == -1){
1009
return '"' + res + '"'
1010
}
1011
var qesc = new RegExp("'", "g") // to escape single quote
1012
res = "'" + res.replace(qesc, "\\'") + "'"
1016
str.__rmul__ = function(self, other){
1017
if(_b_.isinstance(other, _b_.int)){
1018
other = _b_.int.numerator(other)
1019
var res = ''
1020
while(other > 0){
1021
res += self
1022
other--
1023
}
1024
return res
1025
}
1026
return _b_.NotImplemented
1027
}
1028
1029
str.__setattr__ = function(self, attr, value){
1030
if(typeof self === "string"){
1031
if(str.hasOwnProperty(attr)){
1033
attr + "' is read-only")
1034
}else{
1035
throw _b_.AttributeError.$factory(
1036
"'str' object has no attribute '" + attr + "'")
1037
}
1038
}
1039
// str subclass : use __dict__
1040
_b_.dict.$setitem(self.__dict__, attr, value)
1041
return $N
1042
}
1043
1044
str.__setitem__ = function(self, attr, value){
1045
throw _b_.TypeError.$factory(
1046
"'str' object does not support item assignment")
1049
var combining = []
1050
for(var cp = 0x300; cp <= 0x36F; cp++){
1051
combining.push(String.fromCharCode(cp))
1052
}
1056
var repl = '',
1057
chars = to_chars(self)
1058
if(chars.length == self.length){
1059
return self.replace(combining_re, "\u200B$1")
1060
}
1061
for(var i = 0; i < chars.length; i++){
1062
var cp = _b_.ord(chars[i])
1063
if(cp >= 0x300 && cp <= 0x36F){
1064
repl += "\u200B" + chars[i]
1065
}else{
1066
repl += chars[i]
1067
}
1068
}
1069
return repl
1079
$comp_func += "" // source code
1080
var $comps = {">": "gt", ">=": "ge", "<": "lt", "<=": "le"}
1091
str.capitalize = function(self){
1092
var $ = $B.args("capitalize", 1, {self}, ["self"],
1093
arguments, {}, null, null)
1097
return self.charAt(0).toUpperCase() + self.substr(1)
1098
}
1099
1100
str.casefold = function(self){
1101
var $ = $B.args("casefold", 1, {self}, ["self"],
1102
arguments, {}, null, null),
1103
res = "",
1104
char,
1105
cf,
1106
chars = to_chars($.self)
1107
1108
for(var i = 0, len = chars.length; i < len; i++){
1109
char = chars[i]
1110
cf = $B.unicode_casefold[char]
1111
if(cf){
1112
cf.forEach(function(cp){
1113
res += String.fromCharCode(cp)
1114
})
1115
}else{
1122
str.center = function(){
1123
var $ = $B.args("center", 3, {self: null, width: null, fillchar: null},
1124
["self", "width", "fillchar"],
1125
arguments, {fillchar:" "}, null, null),
1126
self = $.self
1130
var pad = parseInt(($.width - self.length) / 2),
1131
res = $.fillchar.repeat(pad)
1140
var $ = $B.args("count", 4, {self:null, sub:null, start:null, stop:null},
1141
["self", "sub", "start", "stop"], arguments, {start:null, stop:null},
1144
throw _b_.TypeError.$factory("Can't convert '" + $B.class_name($.sub) +
1145
"' object to str implicitly")
1146
}
1150
if($.stop !== null){
1151
_slice = _b_.slice.$factory($.start, $.stop)
1152
}else{
1153
_slice = _b_.slice.$factory($.start, $.self.length)
1154
}
1162
if($.start == $.self.length){
1163
return 1
1164
}else if(substr.length == 0){
1165
return 0
1166
}
1169
var n = 0,
1170
pos = 0
1171
while(pos < substr.length){
1172
pos = substr.indexOf($.sub, pos)
1183
str.encode = function(){
1184
var $ = $B.args("encode", 3, {self: null, encoding: null, errors: null},
1185
["self", "encoding", "errors"], arguments,
1186
{encoding: "utf-8", errors: "strict"}, null, null)
1187
if($.encoding == "rot13" || $.encoding == "rot_13"){
1190
for(var i = 0, len = $.self.length; i < len ; i++){
1191
var char = $.self.charAt(i)
1192
if(("a" <= char && char <= "m") || ("A" <= char && char <= "M")){
1193
res += String.fromCharCode(String.charCodeAt(char) + 13)
1194
}else if(("m" < char && char <= "z") ||
1195
("M" < char && char <= "Z")){
1196
res += String.fromCharCode(String.charCodeAt(char) - 13)
1205
// Return True if the string ends with the specified suffix, otherwise
1206
// return False. suffix can also be a tuple of suffixes to look for.
1207
// With optional start, test beginning at that position. With optional
1211
["self", "suffix", "start", "end"],
1212
arguments, {start: 0, end: null}, null, null)
1225
if(! _b_.isinstance(suffix, str)){
1226
throw _b_.TypeError.$factory(
1227
"endswith first arg must be str or a tuple of str, not int")
1228
}
1238
var $ = $B.args("expandtabs", 2, {self: null, tabsize: null},
1239
["self", "tabsize"], arguments, {tabsize: 8}, null, null)
1240
var s = $B.$GetInt($.tabsize),
1241
col = 0,
1242
pos = 0,
1243
res = "",
1244
chars = to_chars(self)
1245
if(s == 1){
1246
return self.replace(/\t/g," ")
1247
}
1248
while(pos < chars.length){
1249
var car = chars[pos]
1259
res += car
1260
col = 0
1261
break
1262
default:
1263
res += car
1264
col++
1265
break
1266
}
1267
pos++
1268
}
1269
return res
1273
// Return the lowest index in the string where substring sub is found,
1274
// such that sub is contained in the slice s[start:end]. Optional
1275
// arguments start and end are interpreted as in slice notation.
1278
{self: null, sub: null, start: null, end: null},
1279
["self", "sub", "start", "end"],
1280
arguments, {start: 0, end: null}, null, null)
1294
var last_search = len - sub_len
1295
for(var i = $.start; i <= last_search; i++){
1296
var js_pos = pypos2jspos($.self, i)
1297
if($.self.substr(js_pos, $.sub.length) == $.sub){
1298
return i
1310
// a.x[z]!r:...
1311
// the object has attributes :
1312
// - name : "a"
1313
// - name_ext : [".x", "[z]"]
1314
// - conv : r
1315
// - spec : rest of string after :
1323
// No : in the string : it only contains a name
1324
name = fmt_string
1325
}else{
1326
// name is before the first ":"
1327
// spec (the format specification) is after
1328
name = elts[0]
1332
var elts = name.split("!")
1333
if(elts.length > 1){
1334
name = elts[0]
1335
conv = elts[1] // conversion flag
1339
// "name' may be a subscription or attribute
1340
// Put these "extensions" in the list "name_ext"
1341
function name_repl(match){
1342
name_ext.push(match)
1344
}
1345
var name_ext_re = /\.[_a-zA-Z][_a-zA-Z0-9]*|\[[_a-zA-Z][_a-zA-Z0-9]*\]|\[[0-9]+\]/g
1346
name = name.replace(name_ext_re, name_repl)
1347
}
1354
// Parse self to detect formatting instructions
1355
// Create a list "parts" made of sections of the string :
1356
// - elements of even rank are literal text
1357
// - elements of odd rank are "format objects", built from the
1358
// format strings in self (of the form {...})
1369
text += "{"
1370
pos += 2
1371
}else if(car == "}" && self.charAt(pos + 1) == "}"){
1378
// Store current literal text
1379
parts.push(text)
1380
1381
// Search the end of the format string, ie the } closing the
1382
// opening {. Since the string can contain other pairs {} for
1383
// nested formatting, an integer nb is incremented for each { and
1384
// decremented for each } ; the end of the format string is
1385
// reached when nb == 0
1386
var end = pos + 1,
1387
nb = 1
1388
while(end < _len){
1389
if(self.charAt(end) == "{"){nb++; end++}
1390
else if(self.charAt(end) == "}"){
1391
nb--; end++
1392
if(nb == 0){
1397
var fmt_obj = $B.parse_format(fmt_string)
1398
fmt_obj.raw_name = fmt_obj.name
1399
fmt_obj.raw_spec = fmt_obj.spec
1433
}else{
1434
text += car
1435
pos++
1436
}
1437
}
1438
if(text){
1439
parts.push(text)
1445
// Special management of keyword arguments if str.format is called by
1446
// str.format_map(mapping) : the argument "mapping" might not be a
1447
// dictionary
1448
var last_arg = $B.last(arguments)
1449
if(last_arg.$nat == "mapping"){
1450
var mapping = last_arg.mapping,
1451
getitem = $B.$getattr(mapping, "__getitem__")
1452
// Get the rest of the arguments
1453
var args = []
1454
for(var i = 0, len = arguments.length - 1; i < len; i++){
1455
args.push(arguments[i])
1456
}
1457
var $ = $B.args("format", 1, {self: null}, ["self"],
1458
args, {}, "$args", null)
1459
}else{
1460
var $ = $B.args("format", 1, {self: null}, ["self"],
1461
arguments, {}, "$args", "$kw"),
1462
mapping = $.$kw, // dictionary
1463
getitem = function(key){
1464
return _b_.dict.$getitem(mapping, key)
1465
}
1466
}
1482
1483
if(fmt.spec !== undefined){
1484
// "spec" may contain "nested replacement fields"
1485
// In this case, evaluate them using the positional
1486
// or keyword arguments passed to format()
1487
function replace_nested(name, key){
1488
if(/\d+/.exec(key)){
1489
// If key is numeric, search in positional
1490
// arguments
1491
return _b_.tuple.__getitem__($.$args,
1492
parseInt(key))
1493
}else{
1494
// Else try in keyword arguments
1495
return _b_.dict.__getitem__($.$kw, key)
1496
}
1497
}
1498
fmt.spec = fmt.spec.replace(/\{(.*?)\}/g,
1499
replace_nested)
1500
}
1502
// Numerical reference : use positional arguments
1503
var pos = parseInt(fmt.name),
1524
// If the conversion flag is set, first call a function to convert
1525
// the value
1526
if(fmt.conv == "a"){value = _b_.ascii(value)}
1527
else if(fmt.conv == "r"){value = _b_.repr(value)}
1528
else if(fmt.conv == "s"){value = _b_.str.$factory(value)}
1541
str.format_map = function(self, mapping){
1542
var $ = $B.args("format_map", 2, {self: null, mapping: null},
1543
['self', 'mapping'], arguments, {}, null, null)
1544
return str.format(self, {$nat: 'mapping', mapping})
1557
/* Return true if the string is empty or all characters in the string are
1558
ASCII, false otherwise. ASCII characters have code points in the range
1559
U+0000-U+007F. */
1568
str.isalnum = function(self){
1569
/* Return true if all characters in the string are alphanumeric and there
1570
is at least one character, false otherwise. A character c is alphanumeric
1571
if one of the following returns True: c.isalpha(), c.isdecimal(),
1572
c.isdigit(), or c.isnumeric(). */
1573
var $ = $B.args("isalnum", 1, {self: null}, ["self"],
1574
arguments, {}, null, null),
1575
cp
1576
for(var char of to_chars(self)){
1577
cp = _b_.ord(char)
1578
if(unicode_tables.Ll[cp] ||
1579
unicode_tables.Lu[cp] ||
1580
unicode_tables.Lm[cp] ||
1581
unicode_tables.Lt[cp] ||
1582
unicode_tables.Lo[cp] ||
1583
unicode_tables.Nd[cp] ||
1584
unicode_tables.digits[cp] ||
1585
unicode_tables.numeric[cp]){
1586
continue
1587
}
1588
return false
1589
}
1590
return true
1591
}
1592
1593
str.isalpha = function(self){
1594
/* Return true if all characters in the string are alphabetic and there is
1595
at least one character, false otherwise. Alphabetic characters are those
1596
characters defined in the Unicode character database as "Letter", i.e.,
1597
those with general category property being one of "Lm", "Lt", "Lu", "Ll",
1598
or "Lo". */
1599
var $ = $B.args("isalpha", 1, {self: null}, ["self"],
1600
arguments, {}, null, null),
1601
cp
1602
for(var char of to_chars(self)){
1603
cp = _b_.ord(char)
1604
if(unicode_tables.Ll[cp] ||
1605
unicode_tables.Lu[cp] ||
1606
unicode_tables.Lm[cp] ||
1607
unicode_tables.Lt[cp] ||
1608
unicode_tables.Lo[cp]){
1609
continue
1610
}
1611
return false
1612
}
1613
return true
1614
}
1615
1616
str.isdecimal = function(self){
1617
/* Return true if all characters in the string are decimal characters and
1618
there is at least one character, false otherwise. Decimal characters are
1619
those that can be used to form numbers in base 10, e.g. U+0660,
1620
ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in
1621
the Unicode General Category "Nd". */
1622
var $ = $B.args("isdecimal", 1, {self: null}, ["self"],
1623
arguments, {}, null, null),
1624
cp
1625
for(var char of to_chars(self)){
1626
cp = _b_.ord(char)
1627
if(! unicode_tables.Nd[cp]){
1628
return false
1629
}
1630
}
1631
return self.length > 0
1632
}
1633
1634
str.isdigit = function(self){
1635
/* Return true if all characters in the string are digits and there is at
1636
least one character, false otherwise. */
1637
var $ = $B.args("isdigit", 1, {self: null}, ["self"],
1638
arguments, {}, null, null),
1639
cp
1640
for(var char of to_chars(self)){
1641
cp = _b_.ord(char)
1642
if(! unicode_tables.digits[cp]){
1643
return false
1644
}
1645
}
1646
return self.length > 0
1647
}
1648
1649
str.isidentifier = function(self){
1650
/* Return true if the string is a valid identifier according to the
1651
language definition. */
1652
var $ = $B.args("isidentifier", 1, {self: null}, ["self"],
1656
}
1657
var chars = to_chars(self)
1658
if(unicode_tables.XID_Start[_b_.ord(chars[0])] === undefined){
1661
for(var char of chars){
1662
var cp = _b_.ord(char)
1663
if(unicode_tables.XID_Continue[cp] === undefined){
1664
return false
1665
}
1666
}
1667
}
1668
return true
1669
}
1670
1671
str.islower = function(self){
1672
/* Return true if all cased characters 4 in the string are lowercase and
1673
there is at least one cased character, false otherwise. */
1674
var $ = $B.args("islower", 1, {self: null}, ["self"],
1675
arguments, {}, null, null),
1676
has_cased = false,
1679
for(var char of to_chars(self)){
1680
cp = _b_.ord(char)
1681
if(unicode_tables.Ll[cp]){
1682
has_cased = true
1683
continue
1684
}else if(unicode_tables.Lu[cp] || unicode_tables.Lt[cp]){
1685
return false
1686
}
1687
}
1688
return has_cased
1689
}
1690
1691
str.isnumeric = function(self){
1692
/* Return true if all characters in the string are numeric characters, and
1693
there is at least one character, false otherwise. Numeric characters
1694
include digit characters, and all characters that have the Unicode numeric
1695
value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric
1696
characters are those with the property value Numeric_Type=Digit,
1697
Numeric_Type=Decimal or Numeric_Type=Numeric.*/
1698
var $ = $B.args("isnumeric", 1, {self: null}, ["self"],
1699
arguments, {}, null, null)
1708
var unprintable = {},
1709
unprintable_gc = ['Cc', 'Cf', 'Co', 'Cs','Zl', 'Zp', 'Zs']
1710
1711
str.isprintable = function(self){
1712
/* Return true if all characters in the string are printable or the string
1713
is empty, false otherwise. Nonprintable characters are those characters
1714
defined in the Unicode character database as "Other" or "Separator",
1715
excepting the ASCII space (0x20) which is considered printable. */
1716
1717
// Set unprintable if not set yet
1718
if(Object.keys(unprintable).length == 0){
1719
for(var i = 0; i < unprintable_gc.length; i++){
1720
var table = unicode_tables[unprintable_gc[i]]
1731
return false
1732
}
1733
}
1734
return true
1735
}
1736
1737
str.isspace = function(self){
1738
/* Return true if there are only whitespace characters in the string and
1739
there is at least one character, false otherwise.
1740
1741
A character is whitespace if in the Unicode character database, either its
1742
general category is Zs ("Separator, space"), or its bidirectional class is
1743
one of WS, B, or S.*/
1744
var $ = $B.args("isspace", 1, {self: null}, ["self"],
1745
arguments, {}, null, null),
1746
cp
1747
for(var char of to_chars(self)){
1748
cp = _b_.ord(char)
1749
if(! unicode_tables.Zs[cp] &&
1750
$B.unicode_bidi_whitespace.indexOf(cp) == -1){
1751
return false
1752
}
1753
}
1754
return self.length > 0
1755
}
1756
1757
str.istitle = function(self){
1758
/* Return true if the string is a titlecased string and there is at least
1759
one character, for example uppercase characters may only follow uncased
1760
characters and lowercase characters only cased ones. Return false
1761
otherwise. */
1762
var $ = $B.args("istitle", 1, {self: null}, ["self"],
1763
arguments, {}, null, null)
1764
return self.length > 0 && str.title(self) == self
1765
}
1766
1767
str.isupper = function(self){
1768
/* Return true if all cased characters 4 in the string are lowercase and
1769
there is at least one cased character, false otherwise. */
1770
var $ = $B.args("islower", 1, {self: null}, ["self"],
1771
arguments, {}, null, null),
1775
for(var char of to_chars(self)){
1776
cp = _b_.ord(char)
1777
if(unicode_tables.Lu[cp]){
1778
is_upper = true
1779
continue
1780
}else if(unicode_tables.Ll[cp] || unicode_tables.Lt[cp]){
1799
throw _b_.TypeError.$factory("sequence item " + count +
1800
": expected str instance, " + $B.class_name(obj2) +
1801
" found")
1802
}
1815
var $ = $B.args("ljust", 3, {self: null, width: null, fillchar:null},
1816
["self", "width", "fillchar"],
1820
if($.width <= len){
1821
return self
1822
}
1823
return self + $.fillchar.repeat($.width - len)
1826
str.lower = function(self){
1827
var $ = $B.args("lower", 1, {self: null}, ["self"],
1828
arguments, {}, null, null)
1829
return self.toLowerCase()
1830
}
1831
1833
var $ = $B.args("lstrip", 2, {self: null, chars: null}, ["self", "chars"],
1834
arguments, {chars:_b_.None}, null, null)
1835
if($.chars === _b_.None){
1836
return $.self.trimLeft()
1837
}
1838
var chars = to_chars(self)
1839
for(var i = 0, len = chars.length; i < len; i++){
1840
if($.chars.indexOf(chars[i]) === -1){
1841
return chars.slice(i).join('')
1849
var $ = $B.args("maketrans", 3, {x: null, y: null, z: null},
1850
["x", "y", "z"], arguments, {y: null, z: null}, null, null)
1855
// If there is only one argument, it must be a dictionary mapping
1856
// Unicode ordinals (integers) or characters (strings of length 1) to
1857
// Unicode ordinals, strings (of arbitrary lengths) or None. Character
1859
if(! _b_.isinstance($.x, _b_.dict)){
1860
throw _b_.TypeError.$factory(
1861
"maketrans only argument must be a dict")
1864
for(var i = 0, len = items.length; i < len; i++){
1865
var k = items[i][0],
1866
v = items[i][1]
1867
if(! _b_.isinstance(k, _b_.int)){
1868
if(_b_.isinstance(k, _b_.str) && k.length == 1){
1869
k = _b_.ord(k)
1870
}else{throw _b_.TypeError.$factory("dictionary key " + k +
1873
if(v !== _b_.None && ! _b_.isinstance(v, [_b_.int, _b_.str])){
1874
throw _b_.TypeError.$factory("dictionary value " + v +
1882
// and in the resulting dictionary, each character in x will be mapped
1883
// to the character at the same position in y
1886
}else if($.x.length !== $.y.length){
1887
throw _b_.TypeError.$factory(
1888
"maketrans arguments must be strings or same length")
1894
if(! _b_.isinstance($.z, _b_.str)){
1895
throw _b_.TypeError.$factory(
1896
"maketrans third argument must be a string")
1918
var $ = $B.args("partition", 2, {self: null, sep: null}, ["self", "sep"],
1919
arguments, {}, null, null)
1926
if(i == -1){
1927
return _b_.tuple.$factory([$.self, "", ""])
1928
}
1929
return _b_.tuple.$factory([chars.slice(0, i).join(''), $.sep,
1930
chars.slice(i + $.sep.length).join('')])
1933
str.removeprefix = function(){
1934
var $ = $B.args("removeprefix", 2, {self: null, prefix: null},
1935
["self", "prefix"], arguments, {}, null, null)
1936
if(!_b_.isinstance($.prefix, str)){
1937
throw _b_.ValueError.$factory("prefix should be str, not " +
1938
`'${$B.class_name($.prefix)}'`)
1939
}
1940
if(str.startswith($.self, $.prefix)){
1941
return $.self.substr($.prefix.length)
1942
}
1943
return $.self.substr(0)
1944
}
1945
1946
str.removesuffix = function(){
1947
var $ = $B.args("removesuffix", 2, {self: null, prefix: null},
1948
["self", "suffix"], arguments, {}, null, null)
1949
if(!_b_.isinstance($.suffix, str)){
1950
throw _b_.ValueError.$factory("suffix should be str, not " +
1951
`'${$B.class_name($.prefix)}'`)
1952
}
1953
if($.suffix.length > 0 && str.endswith($.self, $.suffix)){
1954
return $.self.substr(0, $.self.length - $.suffix.length)
1955
}
1956
return $.self.substr(0)
1957
}
1958
1959
function $re_escape(str){
1960
var specials = "[.*+?|()$^"
1961
for(var i = 0, len = specials.length; i < len; i++){
1962
var re = new RegExp("\\"+specials.charAt(i), "g")
1963
str = str.replace(re, "\\"+specials.charAt(i))
1964
}
1965
return str
1972
var $ = $B.args("replace", 4,
1973
{self: null, old: null, $$new: null, count: null},
1974
["self", "old", "$$new", "count"],
1975
arguments, {count: -1}, null, null),
1976
count = $.count,
1977
self = $.self,
1978
old = $.old,
1979
_new = $.$$new
1981
check_str(old, "replace() argument 1 ")
1982
check_str(_new, "replace() argument 2 ")
1990
if(count == 0){
1991
return self
1992
}
1993
if(count.__class__ == $B.long_int){
1994
count = parseInt(count.value)
1995
}
1997
if(_new == ""){
1998
return self
1999
}
2000
if(self == ""){
2001
return _new
2002
}
2003
var elts = self.split("")
2004
if(count > -1 && elts.length >= count){
2005
var rest = elts.slice(count).join("")
2006
return _new + elts.slice(0, count).join(_new) + rest
2032
res = res.substr(0, pos) + _new + res.substr(pos + old.length)
2033
pos = pos + _new.length
2034
count--
2040
// Return the highest index in the string where substring sub is found,
2041
// such that sub is contained within s[start:end]. Optional arguments
2043
if(arguments.length == 2 && typeof substr == "string"){
2044
return self.lastIndexOf(substr)
2045
}
2047
{self: null, sub: null, start: null, end: null},
2048
["self", "sub", "start", "end"],
2049
arguments, {start: 0, end: null}, null, null)
2054
var len = str.__len__($.self),
2055
sub_len = str.__len__($.sub)
2056
2057
if(sub_len == 0){
2058
if($.js_start > len){
2065
for(var py_pos = $.end - sub_len; py_pos >= $.start; py_pos--){
2066
var js_pos = pypos2jspos($.self, py_pos)
2067
if($.self.substr(js_pos, $.sub.length) == $.sub){
2068
return py_pos
2084
var $ = $B.args("rjust",3,
2085
{self: null, width: null, fillchar: null},
2086
["self", "width", "fillchar"],
2087
arguments, {fillchar: " "}, null, null)
2097
var $ = $B.args("rpartition", 2, {self: null, sep: null}, ["self", "sep"],
2098
arguments, {}, null, null)
2102
var items = str.partition(self, sep).reverse()
2103
for(var i = 0; i < items.length; i++){
2104
items[i] = items[i].split("").reverse().join("")
2110
var $ = $B.args("rsplit", 3, {self: null, sep: null, maxsplit: null},
2111
["self", "sep", "maxsplit"], arguments,
2112
{sep: _b_.None, maxsplit: -1}, null, null),
2113
sep = $.sep
2116
var rev_str = reverse($.self),
2117
rev_sep = sep === _b_.None ? sep : reverse($.sep),
2128
str.rstrip = function(self, x){
2129
var $ = $B.args("rstrip", 2, {self: null, chars: null}, ["self", "chars"],
2131
if($.chars === _b_.None){
2132
return $.self.trimRight()
2133
}
2134
var chars = to_chars(self)
2135
for(var j = chars.length - 1; j >= 0; j--){
2136
if($.chars.indexOf(chars[j]) == -1){
2137
return chars.slice(0, j + 1).join('')
2144
var $ = $B.args("split", 3, {self: null, sep: null, maxsplit: null},
2145
["self", "sep", "maxsplit"], arguments,
2146
{sep: _b_.None, maxsplit: -1}, null, null),
2147
sep = $.sep,
2148
maxsplit = $.maxsplit,
2149
self = $.self,
2150
pos = 0
2151
if(maxsplit.__class__ === $B.long_int){
2152
maxsplit = parseInt(maxsplit.value)
2153
}
2154
if(sep == ""){
2155
throw _b_.ValueError.$factory("empty separator")
2156
}
2160
while(pos < self.length && self.charAt(pos).search(/\s/) > -1){
2161
pos++
2162
}
2163
if(pos === self.length - 1){
2164
return [self]
2165
}
2169
if(name == ""){
2170
name = self.charAt(pos)
2171
}else{
2172
name += self.charAt(pos)
2173
}
2220
str.splitlines = function(self) {
2221
var $ = $B.args('splitlines', 2, {self: null, keepends: null},
2222
['self','keepends'], arguments, {keepends: false},
2223
null, null)
2224
if(!_b_.isinstance($.keepends,[_b_.bool, _b_.int])){
2225
throw _b_.TypeError('integer argument expected, got '+
2228
var keepends = _b_.int.$factory($.keepends),
2229
res = [],
2230
self = $.self,
2231
start = 0,
2232
pos = 0
2233
if(!self.length){
2237
if(self.substr(pos, 2) == '\r\n'){
2238
res.push(self.slice(start, keepends ? pos + 2 : pos))
2239
start = pos = pos+2
2240
}else if(self[pos] == '\r' || self[pos] == '\n'){
2241
res.push(self.slice(start, keepends ? pos+1 : pos))
2242
start = pos = pos+1
2243
}else{
2244
pos++
2245
}
2246
}
2247
if(start < self.length){
2248
res.push(self.slice(start))
2249
}
2250
return res
2254
// Return True if string starts with the prefix, otherwise return False.
2255
// prefix can also be a tuple of prefixes to look for. With optional
2256
// start, test string beginning at that position. With optional end,
2258
var $ = $B.args("startswith", 4,
2259
{self: null, prefix: null, start: null, end: null},
2260
["self", "prefix", "start", "end"],
2261
arguments, {start: 0, end: null}, null, null)
2271
if(! _b_.isinstance(prefix, str)){
2272
throw _b_.TypeError.$factory("endswith first arg must be str " +
2273
"or a tuple of str, not int")
2274
}
2275
if(s.substr(0, prefix.length) == prefix){
2276
return true
2277
}
2283
var $ = $B.args("strip", 2, {self: null, chars: null}, ["self", "chars"],
2284
arguments, {chars: _b_.None}, null, null)
2285
if($.chars === _b_.None){
2286
return $.self.trim()
2287
}
2288
var chars = to_chars($.self)
2289
for(var i = 0; i < chars.length; i++){
2290
if($.chars.indexOf(chars[i]) == -1){
2294
for(var j = chars.length - 1; j >= i; j--){
2295
if($.chars.indexOf(chars[j]) == -1){
2302
str.swapcase = function(self){
2303
var $ = $B.args("swapcase", 1, {self}, ["self"],
2304
arguments, {}, null, null),
2305
res = "",
2306
cp
2307
2308
for(var char of to_chars(self)){
2309
cp = _b_.ord(char)
2310
if(unicode_tables.Ll[cp]){
2311
res += char.toUpperCase()
2312
}else if(unicode_tables.Lu[cp]){
2313
res += char.toLowerCase()
2316
}
2317
}
2318
return res
2319
}
2320
2321
str.title = function(self){
2322
var $ = $B.args("title", 1, {self}, ["self"],
2323
arguments, {}, null, null),
2324
state,
2327
for(var char of to_chars(self)){
2328
cp = _b_.ord(char)
2329
if(unicode_tables.Ll[cp]){
2336
}else if(unicode_tables.Lu[cp] || unicode_tables.Lt[cp]){
2337
res += state ? char.toLowerCase() : char
2349
getitem = $B.$getattr(table, "__getitem__"),
2350
cp
2351
for(var char of to_chars(self)){
2352
cp = _b_.ord(char)
2356
if(typeof repl == "string"){
2357
res.push(repl)
2358
}else if(typeof repl == "number"){
2359
res.push(String.fromCharCode(repl))
2360
}
2369
str.upper = function(self){
2370
var $ = $B.args("upper", 1, {self: null}, ["self"],
2371
arguments, {}, null, null)
2372
return self.toUpperCase()
2373
}
2374
2377
["self", "width"], arguments, {}, null, null),
2378
len = str.__len__(self)
2379
if($.width <= len){
2380
return self
2381
}
2401
if(encoding !== undefined){
2402
// Arguments may be passed as keywords (cf. issue #1060)
2403
var $ = $B.args("str", 3, {arg: null, encoding: null, errors: null},
2404
["arg", "encoding", "errors"], arguments,
2405
{encoding: "utf-8", errors: "strict"}, null, null),
2406
encoding = $.encoding,
2407
errors = $.errors
2408
}
2409
if(typeof arg == "string" || arg instanceof String ||
2410
typeof arg == "number"){
2411
if(isFinite(arg)){
2412
return arg.toString()
2413
}
2419
// class or its subclasses, but the attribute __str__ of the
2420
// class metaclass (usually "type") or its subclasses (usually
2421
// "object")
2422
// The metaclass is the attribute __class__ of the class dictionary
2427
if(arg.__class__ && arg.__class__ === _b_.bytes &&
2428
encoding !== undefined){
2429
// str(bytes, encoding, errors) is equal to
2430
// bytes.decode(encoding, errors)
2433
// Implicit invocation of __str__ uses method __str__ on the class,
2434
// even if arg has an attribute __str__
2435
var klass = arg.__class__ || $B.get_class(arg)
2441
// if not better than object.__str__, try __repr__
2442
(arg.__class__ && arg.__class__ !== _b_.object &&
2443
method.$infos && method.$infos.__func__ === _b_.object.__str__)){
2444
var method = $B.$getattr(klass, "__repr__")
2450
if($B.debug > 1){console.log(err)}
2451
console.log("Warning - no method __str__ or __repr__, " +
2452
"default to toString", arg)
2459
if(cls === undefined){
2460
throw _b_.TypeError.$factory("str.__new__(): not enough arguments")
2484
var args = [],
2485
pos = 0
2486
if(arguments.length > 0){
2487
var args = [arguments[0].valueOf()],
2488
pos = 1
2489
for(var i = 1, len = arguments.length; i < len; i++){
2490
args[pos++] = arguments[i]
2507
// Function to parse the 2nd argument of format()
2508
$B.parse_format_spec = function(spec){
2512
var pos = 0,
2513
aligns = "<>=^",
2514
digits = "0123456789",
2515
types = "bcdeEfFgGnosxX%",
2517
if(align_pos != -1){
2518
if(spec.charAt(1) && aligns.indexOf(spec.charAt(1)) != -1){
2519
// If the second char is also an alignment specifier, the
2520
// first char is the fill value
2521
this.fill = spec.charAt(0)
2522
this.align = spec.charAt(1)
2523
pos = 2
2524
}else{
2525
// The first character defines alignment : fill defaults to ' '
2540
if(car == "+" || car == "-" || car == " "){
2541
this.sign = car
2542
pos++
2543
car = spec.charAt(pos)
2558
if(this.width === undefined){
2559
this.width = car
2560
}else{
2561
this.width += car
2562
}
2569
if(this.width === undefined && car == "{"){
2570
// Width is determined by a parameter
2571
var end_param_pos = spec.substr(pos).search("}")
2572
this.width = spec.substring(pos, end_param_pos)
2573
console.log("width", "[" + this.width + "]")
2574
pos += end_param_pos + 1
2575
}
2576
if(car == ","){
2577
this.comma = true
2578
pos++
2579
car = spec.charAt(pos)
2580
}
2581
if(car == "."){
2582
if(digits.indexOf(spec.charAt(pos + 1)) == -1){
2583
throw _b_.ValueError.$factory(
2584
"Missing precision in format spec")
2586
this.precision = spec.charAt(pos + 1)
2587
pos += 2
2588
car = spec.charAt(pos)
2589
while(car && digits.indexOf(car) > -1){
2596
if(car && types.indexOf(car) > -1){
2597
this.type = car
2598
pos++
2599
car = spec.charAt(pos)
2600
}
2601
if(pos !== spec.length){
2607
return (this.fill === undefined ? "" : _b_.str.$factory(this.fill)) +
2608
(this.align || "") +
2609
(this.sign || "") +
2610
(this.alternate ? "#" : "") +
2611
(this.sign_aware ? "0" : "") +
2612
(this.width || "") +
2613
(this.comma ? "," : "") +
2614
(this.precision ? "." + this.precision : "") +
2615
(this.type || "")
2620
if(fmt.width && s.length < fmt.width){
2621
var fill = fmt.fill || " ",
2622
align = fmt.align || "<",
2623
missing = fmt.width - s.length
2625
case "<":
2626
return s + fill.repeat(missing)
2627
case ">":
2628
return fill.repeat(missing) + s
2629
case "=":
2630
if("+-".indexOf(s.charAt(0)) > -1){
2631
return s.charAt(0) + fill.repeat(missing) + s.substr(1)
2635
case "^":
2636
var left = parseInt(missing / 2)
2637
return fill.repeat(left) + s + fill.repeat(missing - left)
2650
function fstring_error(msg, pos){
2651
error = Error(msg)
2652
error.position = pos
2653
throw error
2654
}
2655
2656
$B.parse_fstring = function(string){
2657
// Parse a f-string
2658
var elts = [],
2659
pos = 0,
2724
}else if(ctype == "debug"){
2725
// after the equal sign, whitespace are ignored and the only
2726
// valid characters are } and :
2727
while(string.charAt(i) == " "){i++}
2728
if(string.charAt(i) == "}"){
2729
// end of debug expression
2736
}else{
2737
// End of expression is the } matching the opening {
2738
// There may be nested braces
2739
var i = pos,
2740
nb_braces = 1,
2756
if(current.expression == ""){
2757
fstring_error("f-string: empty expression not allowed",
2758
pos)
2759
}
2769
// backslash is not allowed in expressions
2770
throw Error("f-string expression part cannot include a" +
2771
" backslash")
2778
throw Error("f-string: invalid conversion character:" +
2779
" expected 's', 'r', or 'a'")
2780
}else{
2794
if(string.substr(i, 3) == '"""'){
2795
var end = string.indexOf('"""', i + 3)
2796
if(end == -1){
2798
}else{
2799
var trs = string.substring(i, end + 3)
2800
trs = trs.replace("\n", "\\n\\")
2820
var ce = current.expression,
2821
last_char = ce.charAt(ce.length - 1),
2822
last_char_re = ('()'.indexOf(last_char) > -1 ? "\\" : "") + last_char
2831
}else{
2832
// add debug string
2833
tail = car
2834
while(string.charAt(i + 1).match(/\s/)){
2835
tail += string.charAt(i + 1)
2836
i++
2837
}
2839
elts.push(current.expression + tail)
2840
// remove trailing whitespace from expression
2841
while(ce.match(/\s$/)){
2842
ce = ce.substr(0, ce.length - 1)
2843
}
2844
current.expression = ce
2845
ctype = "debug"
2846
i++
2847
}
2861
for(var elt of elts){
2862
if(typeof elt == "object"){
2863
if(elt.fmt_pos !== undefined &&
2864
elt.expression.charAt(elt.fmt_pos) != ':'){
2865
console.log('mauvais format', string, elts)
2866
throw Error()
2867
}
2868
}
2869
}
2874
if(i >= 0x10000 && i <= 0x10FFFF){
2875
var code = (i - 0x10000)
2876
return String.fromCodePoint(0xD800 | (code >> 10)) +
2877
String.fromCodePoint(0xDC00 | (code & 0x3FF))
2878
}else{
2879
return String.fromCodePoint(i)
2880
}
2881
}
2884
if(c.length == 1){
2885
return c.charCodeAt(0)
2886
}
2887
var code = 0x10000
2888
code += (c.charCodeAt(0) & 0x03FF) << 10
2889
code += (c.charCodeAt(1) & 0x03FF)
2890
return code
2891
}
2892