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 16, 2021
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
2774 lines (2563 sloc)
84.1 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.__setattr__ = function(self, attr, value){
933
if(typeof self === "string"){
934
if(str.hasOwnProperty(attr)){
935
throw _b_.AttributeError.$factory("'str' object attribute '" +
936
attr + "' is read-only")
937
}else{
938
throw _b_.AttributeError.$factory(
939
"'str' object has no attribute '" + attr + "'")
940
}
941
}
942
// str subclass : use __dict__
943
_b_.dict.$setitem(self.__dict__, attr, value)
944
return $N
945
}
946
947
str.__setitem__ = function(self, attr, value){
948
throw _b_.TypeError.$factory(
949
"'str' object does not support item assignment")
952
var combining = []
953
for(var cp = 0x300; cp <= 0x36F; cp++){
954
combining.push(String.fromCharCode(cp))
955
}
959
var repl = '',
960
chars = to_chars(self)
961
if(chars.length == self.length){
962
return self.replace(combining_re, "\u200B$1")
963
}
964
for(var i = 0; i < chars.length; i++){
965
var cp = _b_.ord(chars[i])
966
if(cp >= 0x300 && cp <= 0x36F){
967
repl += "\u200B" + chars[i]
968
}else{
969
repl += chars[i]
970
}
971
}
972
return repl
982
$comp_func += "" // source code
983
var $comps = {">": "gt", ">=": "ge", "<": "lt", "<=": "le"}
992
var $notimplemented = function(self, other){
993
throw NotImplementedError.$factory(
994
"OPERATOR not implemented for class str")
997
str.capitalize = function(self){
998
var $ = $B.args("capitalize", 1, {self}, ["self"],
999
arguments, {}, null, null)
1003
return self.charAt(0).toUpperCase() + self.substr(1)
1004
}
1005
1006
str.casefold = function(self){
1007
var $ = $B.args("casefold", 1, {self}, ["self"],
1008
arguments, {}, null, null),
1009
res = "",
1010
char,
1011
cf,
1012
chars = to_chars($.self)
1013
1014
for(var i = 0, len = chars.length; i < len; i++){
1015
char = chars[i]
1016
cf = $B.unicode_casefold[char]
1017
if(cf){
1018
cf.forEach(function(cp){
1019
res += String.fromCharCode(cp)
1020
})
1021
}else{
1028
str.center = function(){
1029
var $ = $B.args("center", 3, {self: null, width: null, fillchar: null},
1030
["self", "width", "fillchar"],
1031
arguments, {fillchar:" "}, null, null),
1032
self = $.self
1036
var pad = parseInt(($.width - self.length) / 2),
1037
res = $.fillchar.repeat(pad)
1046
var $ = $B.args("count", 4, {self:null, sub:null, start:null, stop:null},
1047
["self", "sub", "start", "stop"], arguments, {start:null, stop:null},
1049
if(!(typeof $.sub == "string")){
1050
throw _b_.TypeError.$factory("Can't convert '" + $B.class_name($.sub) +
1051
"' object to str implicitly")
1052
}
1056
if($.stop !== null){
1057
_slice = _b_.slice.$factory($.start, $.stop)
1058
}else{
1059
_slice = _b_.slice.$factory($.start, $.self.length)
1060
}
1068
if($.start == $.self.length){
1069
return 1
1070
}else if(substr.length == 0){
1071
return 0
1072
}
1075
var n = 0,
1076
pos = 0
1077
while(pos < substr.length){
1078
pos = substr.indexOf($.sub, pos)
1089
str.encode = function(){
1090
var $ = $B.args("encode", 3, {self: null, encoding: null, errors: null},
1091
["self", "encoding", "errors"], arguments,
1092
{encoding: "utf-8", errors: "strict"}, null, null)
1093
if($.encoding == "rot13" || $.encoding == "rot_13"){
1096
for(var i = 0, len = $.self.length; i < len ; i++){
1097
var char = $.self.charAt(i)
1098
if(("a" <= char && char <= "m") || ("A" <= char && char <= "M")){
1099
res += String.fromCharCode(String.charCodeAt(char) + 13)
1100
}else if(("m" < char && char <= "z") ||
1101
("M" < char && char <= "Z")){
1102
res += String.fromCharCode(String.charCodeAt(char) - 13)
1111
// Return True if the string ends with the specified suffix, otherwise
1112
// return False. suffix can also be a tuple of suffixes to look for.
1113
// With optional start, test beginning at that position. With optional
1117
["self", "suffix", "start", "end"],
1118
arguments, {start: 0, end: null}, null, null)
1131
if(! _b_.isinstance(suffix, str)){
1132
throw _b_.TypeError.$factory(
1133
"endswith first arg must be str or a tuple of str, not int")
1134
}
1144
var $ = $B.args("expandtabs", 2, {self: null, tabsize: null},
1145
["self", "tabsize"], arguments, {tabsize: 8}, null, null)
1146
var s = $B.$GetInt($.tabsize),
1147
col = 0,
1148
pos = 0,
1149
res = "",
1150
chars = to_chars(self)
1151
if(s == 1){
1152
return self.replace(/\t/g," ")
1153
}
1154
while(pos < chars.length){
1155
var car = chars[pos]
1165
res += car
1166
col = 0
1167
break
1168
default:
1169
res += car
1170
col++
1171
break
1172
}
1173
pos++
1174
}
1175
return res
1179
// Return the lowest index in the string where substring sub is found,
1180
// such that sub is contained in the slice s[start:end]. Optional
1181
// arguments start and end are interpreted as in slice notation.
1184
{self: null, sub: null, start: null, end: null},
1185
["self", "sub", "start", "end"],
1186
arguments, {start: 0, end: null}, null, null)
1190
if(!isinstance($.start, _b_.int)||!isinstance($.end, _b_.int)){
1191
throw _b_.TypeError.$factory("slice indices must be " +
1192
"integers or None or have an __index__ method")}
1193
// Can't use string.substring(start, end) because if end < start,
1194
// Javascript transforms it into substring(end, start)...
1195
var s = ""
1196
for(var i = $.start; i < $.end; i++){
1197
s += $.self.charAt(i)
1198
}
1200
var len = str.__len__($.self)
1201
1202
if($.sub.length == 0 && $.start == len){
1203
return len
1204
}
1205
if(s.length + $.sub.length == 0){
1206
return -1
1207
}
1209
var last_search = s.length - $.sub.length
1210
for(var i = 0; i <= last_search; i++){
1211
if(s.substr(i, $.sub.length) == $.sub){
1212
return $.start + str.__len__(s.substr(0, i))
1213
}
1224
// a.x[z]!r:...
1225
// the object has attributes :
1226
// - name : "a"
1227
// - name_ext : [".x", "[z]"]
1228
// - conv : r
1229
// - spec : rest of string after :
1237
// No : in the string : it only contains a name
1238
name = fmt_string
1239
}else{
1240
// name is before the first ":"
1241
// spec (the format specification) is after
1242
name = elts[0]
1246
var elts = name.split("!")
1247
if(elts.length > 1){
1248
name = elts[0]
1249
conv = elts[1] // conversion flag
1253
// "name' may be a subscription or attribute
1254
// Put these "extensions" in the list "name_ext"
1255
function name_repl(match){
1256
name_ext.push(match)
1258
}
1259
var name_ext_re = /\.[_a-zA-Z][_a-zA-Z0-9]*|\[[_a-zA-Z][_a-zA-Z0-9]*\]|\[[0-9]+\]/g
1260
name = name.replace(name_ext_re, name_repl)
1261
}
1268
// Parse self to detect formatting instructions
1269
// Create a list "parts" made of sections of the string :
1270
// - elements of even rank are literal text
1271
// - elements of odd rank are "format objects", built from the
1272
// format strings in self (of the form {...})
1283
text += "{"
1284
pos += 2
1285
}else if(car == "}" && self.charAt(pos + 1) == "}"){
1292
// Store current literal text
1293
parts.push(text)
1294
1295
// Search the end of the format string, ie the } closing the
1296
// opening {. Since the string can contain other pairs {} for
1297
// nested formatting, an integer nb is incremented for each { and
1298
// decremented for each } ; the end of the format string is
1299
// reached when nb == 0
1300
var end = pos + 1,
1301
nb = 1
1302
while(end < _len){
1303
if(self.charAt(end) == "{"){nb++; end++}
1304
else if(self.charAt(end) == "}"){
1305
nb--; end++
1306
if(nb == 0){
1311
var fmt_obj = $B.parse_format(fmt_string)
1312
fmt_obj.raw_name = fmt_obj.name
1313
fmt_obj.raw_spec = fmt_obj.spec
1352
// Special management of keyword arguments if str.format is called by
1353
// str.format_map(mapping) : the argument "mapping" might not be a
1354
// dictionary
1355
var last_arg = $B.last(arguments)
1356
if(last_arg.$nat == "mapping"){
1357
var mapping = last_arg.mapping,
1358
getitem = $B.$getattr(mapping, "__getitem__")
1359
// Get the rest of the arguments
1360
var args = []
1361
for(var i = 0, len = arguments.length - 1; i < len; i++){
1362
args.push(arguments[i])
1363
}
1364
var $ = $B.args("format", 1, {self: null}, ["self"],
1365
args, {}, "$args", null)
1366
}else{
1367
var $ = $B.args("format", 1, {self: null}, ["self"],
1368
arguments, {}, "$args", "$kw"),
1369
mapping = $.$kw, // dictionary
1370
getitem = function(key){
1371
return _b_.dict.$getitem(mapping, key)
1372
}
1373
}
1386
1387
if(fmt.spec !== undefined){
1388
// "spec" may contain "nested replacement fields"
1389
// In this case, evaluate them using the positional
1390
// or keyword arguments passed to format()
1391
function replace_nested(name, key){
1392
if(/\d+/.exec(key)){
1393
// If key is numeric, search in positional
1394
// arguments
1395
return _b_.tuple.__getitem__($.$args,
1396
parseInt(key))
1397
}else{
1398
// Else try in keyword arguments
1399
return _b_.dict.__getitem__($.$kw, key)
1400
}
1401
}
1402
fmt.spec = fmt.spec.replace(/\{(.*?)\}/g,
1403
replace_nested)
1404
}
1406
// Numerical reference : use positional arguments
1407
var pos = parseInt(fmt.name),
1417
// Attribute
1418
value = _b_.getattr(value, ext.substr(1))
1419
}else{
1420
// Subscription
1423
if(key.charAt(0).search(/\d/) > -1){key = parseInt(key)}
1424
value = _b_.getattr(value, "__getitem__")(key)
1428
// If the conversion flag is set, first call a function to convert
1429
// the value
1430
if(fmt.conv == "a"){value = _b_.ascii(value)}
1431
else if(fmt.conv == "r"){value = _b_.repr(value)}
1432
else if(fmt.conv == "s"){value = _b_.str.$factory(value)}
1445
str.format_map = function(self, mapping){
1446
var $ = $B.args("format_map", 2, {self: null, mapping: null},
1447
['self', 'mapping'], arguments, {}, null, null)
1448
return str.format(self, {$nat: 'mapping', mapping})
1461
/* Return true if the string is empty or all characters in the string are
1462
ASCII, false otherwise. ASCII characters have code points in the range
1463
U+0000-U+007F. */
1472
str.isalnum = function(self){
1473
/* Return true if all characters in the string are alphanumeric and there
1474
is at least one character, false otherwise. A character c is alphanumeric
1475
if one of the following returns True: c.isalpha(), c.isdecimal(),
1476
c.isdigit(), or c.isnumeric(). */
1477
var $ = $B.args("isalnum", 1, {self: null}, ["self"],
1478
arguments, {}, null, null),
1479
cp
1480
for(var char of to_chars(self)){
1481
cp = _b_.ord(char)
1482
if(unicode_tables.Ll[cp] ||
1483
unicode_tables.Lu[cp] ||
1484
unicode_tables.Lm[cp] ||
1485
unicode_tables.Lt[cp] ||
1486
unicode_tables.Lo[cp] ||
1487
unicode_tables.Nd[cp] ||
1488
unicode_tables.digits[cp] ||
1489
unicode_tables.numeric[cp]){
1490
continue
1491
}
1492
return false
1493
}
1494
return true
1495
}
1496
1497
str.isalpha = function(self){
1498
/* Return true if all characters in the string are alphabetic and there is
1499
at least one character, false otherwise. Alphabetic characters are those
1500
characters defined in the Unicode character database as "Letter", i.e.,
1501
those with general category property being one of "Lm", "Lt", "Lu", "Ll",
1502
or "Lo". */
1503
var $ = $B.args("isalpha", 1, {self: null}, ["self"],
1504
arguments, {}, null, null),
1505
cp
1506
for(var char of to_chars(self)){
1507
cp = _b_.ord(char)
1508
if(unicode_tables.Ll[cp] ||
1509
unicode_tables.Lu[cp] ||
1510
unicode_tables.Lm[cp] ||
1511
unicode_tables.Lt[cp] ||
1512
unicode_tables.Lo[cp]){
1513
continue
1514
}
1515
return false
1516
}
1517
return true
1518
}
1519
1520
str.isdecimal = function(self){
1521
/* Return true if all characters in the string are decimal characters and
1522
there is at least one character, false otherwise. Decimal characters are
1523
those that can be used to form numbers in base 10, e.g. U+0660,
1524
ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in
1525
the Unicode General Category "Nd". */
1526
var $ = $B.args("isdecimal", 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.Nd[cp]){
1532
return false
1533
}
1534
}
1535
return self.length > 0
1536
}
1537
1538
str.isdigit = function(self){
1539
/* Return true if all characters in the string are digits and there is at
1540
least one character, false otherwise. */
1541
var $ = $B.args("isdigit", 1, {self: null}, ["self"],
1542
arguments, {}, null, null),
1543
cp
1544
for(var char of to_chars(self)){
1545
cp = _b_.ord(char)
1546
if(! unicode_tables.digits[cp]){
1547
return false
1548
}
1549
}
1550
return self.length > 0
1551
}
1552
1553
str.isidentifier = function(self){
1554
/* Return true if the string is a valid identifier according to the
1555
language definition. */
1556
var $ = $B.args("isidentifier", 1, {self: null}, ["self"],
1560
}
1561
var chars = to_chars(self)
1562
if(unicode_tables.XID_Start[_b_.ord(chars[0])] === undefined){
1565
for(var char of chars){
1566
var cp = _b_.ord(char)
1567
if(unicode_tables.XID_Continue[cp] === undefined){
1568
return false
1569
}
1570
}
1571
}
1572
return true
1573
}
1574
1575
str.islower = function(self){
1576
/* Return true if all cased characters 4 in the string are lowercase and
1577
there is at least one cased character, false otherwise. */
1578
var $ = $B.args("islower", 1, {self: null}, ["self"],
1579
arguments, {}, null, null),
1580
has_cased = false,
1583
for(var char of to_chars(self)){
1584
cp = _b_.ord(char)
1585
if(unicode_tables.Ll[cp]){
1586
has_cased = true
1587
continue
1588
}else if(unicode_tables.Lu[cp] || unicode_tables.Lt[cp]){
1589
return false
1590
}
1591
}
1592
return has_cased
1593
}
1594
1595
str.isnumeric = function(self){
1596
/* Return true if all characters in the string are numeric characters, and
1597
there is at least one character, false otherwise. Numeric characters
1598
include digit characters, and all characters that have the Unicode numeric
1599
value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric
1600
characters are those with the property value Numeric_Type=Digit,
1601
Numeric_Type=Decimal or Numeric_Type=Numeric.*/
1602
var $ = $B.args("isnumeric", 1, {self: null}, ["self"],
1603
arguments, {}, null, null)
1612
var unprintable = {},
1613
unprintable_gc = ['Cc', 'Cf', 'Co', 'Cs','Zl', 'Zp', 'Zs']
1614
1615
str.isprintable = function(self){
1616
/* Return true if all characters in the string are printable or the string
1617
is empty, false otherwise. Nonprintable characters are those characters
1618
defined in the Unicode character database as "Other" or "Separator",
1619
excepting the ASCII space (0x20) which is considered printable. */
1620
1621
// Set unprintable if not set yet
1622
if(Object.keys(unprintable).length == 0){
1623
for(var i = 0; i < unprintable_gc.length; i++){
1624
var table = unicode_tables[unprintable_gc[i]]
1635
return false
1636
}
1637
}
1638
return true
1639
}
1640
1641
str.isspace = function(self){
1642
/* Return true if there are only whitespace characters in the string and
1643
there is at least one character, false otherwise.
1644
1645
A character is whitespace if in the Unicode character database, either its
1646
general category is Zs ("Separator, space"), or its bidirectional class is
1647
one of WS, B, or S.*/
1648
var $ = $B.args("isspace", 1, {self: null}, ["self"],
1649
arguments, {}, null, null),
1650
cp
1651
for(var char of to_chars(self)){
1652
cp = _b_.ord(char)
1653
if(! unicode_tables.Zs[cp] &&
1654
$B.unicode_bidi_whitespace.indexOf(cp) == -1){
1655
return false
1656
}
1657
}
1658
return self.length > 0
1659
}
1660
1661
str.istitle = function(self){
1662
/* Return true if the string is a titlecased string and there is at least
1663
one character, for example uppercase characters may only follow uncased
1664
characters and lowercase characters only cased ones. Return false
1665
otherwise. */
1666
var $ = $B.args("istitle", 1, {self: null}, ["self"],
1667
arguments, {}, null, null)
1668
return self.length > 0 && str.title(self) == self
1669
}
1670
1671
str.isupper = 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),
1679
for(var char of to_chars(self)){
1680
cp = _b_.ord(char)
1681
if(unicode_tables.Lu[cp]){
1682
is_upper = true
1683
continue
1684
}else if(unicode_tables.Ll[cp] || unicode_tables.Lt[cp]){
1702
if(! isinstance(obj2, str)){
1703
throw _b_.TypeError.$factory("sequence item " + count +
1704
": expected str instance, " + $B.class_name(obj2) +
1705
" found")
1706
}
1719
var $ = $B.args("ljust", 3, {self: null, width: null, fillchar:null},
1720
["self", "width", "fillchar"],
1724
if($.width <= len){
1725
return self
1726
}
1727
return self + $.fillchar.repeat($.width - len)
1730
str.lower = function(self){
1731
var $ = $B.args("lower", 1, {self: null}, ["self"],
1732
arguments, {}, null, null)
1733
return self.toLowerCase()
1734
}
1735
1737
var $ = $B.args("lstrip", 2, {self: null, chars: null}, ["self", "chars"],
1738
arguments, {chars:_b_.None}, null, null)
1739
if($.chars === _b_.None){
1740
return $.self.trimLeft()
1741
}
1742
var chars = to_chars(self)
1743
for(var i = 0, len = chars.length; i < len; i++){
1744
if($.chars.indexOf(chars[i]) === -1){
1745
return chars.slice(i).join('')
1753
var $ = $B.args("maketrans", 3, {x: null, y: null, z: null},
1754
["x", "y", "z"], arguments, {y: null, z: null}, null, null)
1759
// If there is only one argument, it must be a dictionary mapping
1760
// Unicode ordinals (integers) or characters (strings of length 1) to
1761
// Unicode ordinals, strings (of arbitrary lengths) or None. Character
1763
if(! _b_.isinstance($.x, _b_.dict)){
1764
throw _b_.TypeError.$factory(
1765
"maketrans only argument must be a dict")
1768
for(var i = 0, len = items.length; i < len; i++){
1769
var k = items[i][0],
1770
v = items[i][1]
1771
if(! _b_.isinstance(k, _b_.int)){
1772
if(_b_.isinstance(k, _b_.str) && k.length == 1){
1773
k = _b_.ord(k)
1774
}else{throw _b_.TypeError.$factory("dictionary key " + k +
1777
if(v !== _b_.None && ! _b_.isinstance(v, [_b_.int, _b_.str])){
1778
throw _b_.TypeError.$factory("dictionary value " + v +
1786
// and in the resulting dictionary, each character in x will be mapped
1787
// to the character at the same position in y
1790
}else if($.x.length !== $.y.length){
1791
throw _b_.TypeError.$factory(
1792
"maketrans arguments must be strings or same length")
1798
if(! _b_.isinstance($.z, _b_.str)){
1799
throw _b_.TypeError.$factory(
1800
"maketrans third argument must be a string")
1822
var $ = $B.args("partition", 2, {self: null, sep: null}, ["self", "sep"],
1823
arguments, {}, null, null)
1828
var chars = to_chars(self),
1829
i = chars.indexOf($.sep)
1830
if(i == -1){
1831
return _b_.tuple.$factory([$.self, "", ""])
1832
}
1833
return _b_.tuple.$factory([chars.slice(0, i).join(''), $.sep,
1834
chars.slice(i + $.sep.length).join('')])
1837
str.removeprefix = function(){
1838
var $ = $B.args("removeprefix", 2, {self: null, prefix: null},
1839
["self", "prefix"], arguments, {}, null, null)
1840
if(!_b_.isinstance($.prefix, str)){
1841
throw _b_.ValueError.$factory("prefix should be str, not " +
1842
`'${$B.class_name($.prefix)}'`)
1843
}
1844
if(str.startswith($.self, $.prefix)){
1845
return $.self.substr($.prefix.length)
1846
}
1847
return $.self.substr(0)
1848
}
1849
1850
str.removesuffix = function(){
1851
var $ = $B.args("removesuffix", 2, {self: null, prefix: null},
1852
["self", "suffix"], arguments, {}, null, null)
1853
if(!_b_.isinstance($.suffix, str)){
1854
throw _b_.ValueError.$factory("suffix should be str, not " +
1855
`'${$B.class_name($.prefix)}'`)
1856
}
1857
if($.suffix.length > 0 && str.endswith($.self, $.suffix)){
1858
return $.self.substr(0, $.self.length - $.suffix.length)
1859
}
1860
return $.self.substr(0)
1861
}
1862
1863
function $re_escape(str){
1864
var specials = "[.*+?|()$^"
1865
for(var i = 0, len = specials.length; i < len; i++){
1866
var re = new RegExp("\\"+specials.charAt(i), "g")
1867
str = str.replace(re, "\\"+specials.charAt(i))
1868
}
1869
return str
1876
var $ = $B.args("replace", 4,
1877
{self: null, old: null, $$new: null, count: null},
1878
["self", "old", "$$new", "count"],
1879
arguments, {count: -1}, null, null),
1880
count = $.count,
1881
self = $.self,
1882
old = $.old,
1883
_new = $.$$new
1890
"' object cannot be interpreted as an integer")
1891
}else if(isinstance(count, _b_.float)){
1892
throw _b_.TypeError.$factory("integer argument expected, got float")
1893
}
1894
if(count == 0){
1895
return self
1896
}
1897
if(count.__class__ == $B.long_int){
1898
count = parseInt(count.value)
1899
}
1901
if(_new == ""){
1902
return self
1903
}
1904
if(self == ""){
1905
return _new
1906
}
1907
var elts = self.split("")
1908
if(count > -1 && elts.length >= count){
1909
var rest = elts.slice(count).join("")
1910
return _new + elts.slice(0, count).join(_new) + rest
1936
res = res.substr(0, pos) + _new + res.substr(pos + old.length)
1937
pos = pos + _new.length
1938
count--
1944
// Return the highest index in the string where substring sub is found,
1945
// such that sub is contained within s[start:end]. Optional arguments
1947
if(arguments.length == 2 && typeof substr == "string"){
1948
return self.lastIndexOf(substr)
1949
}
1951
{self: null, sub: null, start: null, end: null},
1952
["self", "sub", "start", "end"],
1953
arguments, {start: 0, end: null}, null, null)
1960
if($.start > $.self.length){
1961
return -1
1962
}else{
1963
return str.__len__($.self)
1964
}
1970
if($.self.substr(i, sublen) == $.sub){
1971
return str.__len__($.self.substr(0, i))
1972
}
1987
var $ = $B.args("rjust",3,
1988
{self: null, width: null, fillchar: null},
1989
["self", "width", "fillchar"],
1990
arguments, {fillchar: " "}, null, null)
2000
var $ = $B.args("rpartition", 2, {self: null, sep: null}, ["self", "sep"],
2001
arguments, {}, null, null)
2005
var items = str.partition(self, sep).reverse()
2006
for(var i = 0; i < items.length; i++){
2007
items[i] = items[i].split("").reverse().join("")
2013
var $ = $B.args("rsplit", 3, {self: null, sep: null, maxsplit: null},
2014
["self", "sep", "maxsplit"], arguments,
2015
{sep: _b_.None, maxsplit: -1}, null, null),
2016
sep = $.sep
2019
var rev_str = reverse($.self),
2020
rev_sep = sep === _b_.None ? sep : reverse($.sep),
2031
str.rstrip = function(self, x){
2032
var $ = $B.args("rstrip", 2, {self: null, chars: null}, ["self", "chars"],
2034
if($.chars === _b_.None){
2035
return $.self.trimRight()
2036
}
2037
var chars = to_chars(self)
2038
for(var j = chars.length - 1; j >= 0; j--){
2039
if($.chars.indexOf(chars[j]) == -1){
2040
return chars.slice(0, j + 1).join('')
2047
var $ = $B.args("split", 3, {self: null, sep: null, maxsplit: null},
2048
["self", "sep", "maxsplit"], arguments,
2049
{sep: _b_.None, maxsplit: -1}, null, null),
2050
sep = $.sep,
2051
maxsplit = $.maxsplit,
2052
self = $.self,
2053
pos = 0
2054
if(maxsplit.__class__ === $B.long_int){
2055
maxsplit = parseInt(maxsplit.value)
2056
}
2057
if(sep == ""){
2058
throw _b_.ValueError.$factory("empty separator")
2059
}
2062
while(pos < self.length && self.charAt(pos).search(/\s/) > -1){
2063
pos++
2064
}
2065
if(pos === self.length - 1){
2066
return [self]
2067
}
2071
if(name == ""){
2072
name = self.charAt(pos)
2073
}else{
2074
name += self.charAt(pos)
2075
}
2097
var res = [],
2098
s = "",
2099
seplen = sep.length
2100
if(maxsplit == 0){return [self]}
2101
while(pos < self.length){
2102
if(self.substr(pos, seplen) == sep){
2120
str.splitlines = function(self) {
2121
var $ = $B.args('splitlines', 2, {self: null, keepends: null},
2122
['self','keepends'], arguments, {keepends: false},
2123
null, null)
2124
if(!_b_.isinstance($.keepends,[_b_.bool, _b_.int])){
2125
throw _b_.TypeError('integer argument expected, got '+
2128
var keepends = _b_.int.$factory($.keepends),
2129
res = [],
2130
self = $.self,
2131
start = 0,
2132
pos = 0
2133
if(!self.length){
2137
if(self.substr(pos, 2) == '\r\n'){
2138
res.push(self.slice(start, keepends ? pos + 2 : pos))
2139
start = pos = pos+2
2140
}else if(self[pos] == '\r' || self[pos] == '\n'){
2141
res.push(self.slice(start, keepends ? pos+1 : pos))
2142
start = pos = pos+1
2143
}else{
2144
pos++
2145
}
2146
}
2147
if(start < self.length){
2148
res.push(self.slice(start))
2149
}
2150
return res
2154
// Return True if string starts with the prefix, otherwise return False.
2155
// prefix can also be a tuple of prefixes to look for. With optional
2156
// start, test string beginning at that position. With optional end,
2158
var $ = $B.args("startswith", 4,
2159
{self: null, prefix: null, start: null, end: null},
2160
["self", "prefix", "start", "end"],
2161
arguments, {start: 0, end: null}, null, null)
2173
if(! _b_.isinstance(prefix, str)){
2174
throw _b_.TypeError.$factory("endswith first arg must be str " +
2175
"or a tuple of str, not int")
2176
}
2177
if(s.substr(0, prefix.length) == prefix){
2178
return true
2179
}
2185
var $ = $B.args("strip", 2, {self: null, chars: null}, ["self", "chars"],
2186
arguments, {chars: _b_.None}, null, null)
2187
if($.chars === _b_.None){
2188
return $.self.trim()
2189
}
2190
var chars = to_chars($.self)
2191
for(var i = 0; i < chars.length; i++){
2192
if($.chars.indexOf(chars[i]) == -1){
2196
for(var j = chars.length - 1; j >= i; j--){
2197
if($.chars.indexOf(chars[j]) == -1){
2204
str.swapcase = function(self){
2205
var $ = $B.args("swapcase", 1, {self}, ["self"],
2206
arguments, {}, null, null),
2207
res = "",
2208
cp
2209
2210
for(var char of to_chars(self)){
2211
cp = _b_.ord(char)
2212
if(unicode_tables.Ll[cp]){
2213
res += char.toUpperCase()
2214
}else if(unicode_tables.Lu[cp]){
2215
res += char.toLowerCase()
2218
}
2219
}
2220
return res
2221
}
2222
2223
str.title = function(self){
2224
var $ = $B.args("title", 1, {self}, ["self"],
2225
arguments, {}, null, null),
2226
state,
2229
for(var char of to_chars(self)){
2230
cp = _b_.ord(char)
2231
if(unicode_tables.Ll[cp]){
2238
}else if(unicode_tables.Lu[cp] || unicode_tables.Lt[cp]){
2239
res += state ? char.toLowerCase() : char
2251
getitem = $B.$getattr(table, "__getitem__"),
2252
cp
2253
for(var char of to_chars(self)){
2254
cp = _b_.ord(char)
2258
if(typeof repl == "string"){
2259
res.push(repl)
2260
}else if(typeof repl == "number"){
2261
res.push(String.fromCharCode(repl))
2262
}
2271
str.upper = function(self){
2272
var $ = $B.args("upper", 1, {self: null}, ["self"],
2273
arguments, {}, null, null)
2274
return self.toUpperCase()
2275
}
2276
2279
["self", "width"], arguments, {}, null, null),
2280
len = str.__len__(self)
2281
if($.width <= len){
2282
return self
2283
}
2303
if(encoding !== undefined){
2304
// Arguments may be passed as keywords (cf. issue #1060)
2305
var $ = $B.args("str", 3, {arg: null, encoding: null, errors: null},
2306
["arg", "encoding", "errors"], arguments,
2307
{encoding: "utf-8", errors: "strict"}, null, null),
2308
encoding = $.encoding,
2309
errors = $.errors
2310
}
2321
// class or its subclasses, but the attribute __str__ of the
2322
// class metaclass (usually "type") or its subclasses (usually
2323
// "object")
2324
// The metaclass is the attribute __class__ of the class dictionary
2329
if(arg.__class__ && arg.__class__ === _b_.bytes &&
2330
encoding !== undefined){
2331
// str(bytes, encoding, errors) is equal to
2332
// bytes.decode(encoding, errors)
2335
// Implicit invocation of __str__ uses method __str__ on the class,
2336
// even if arg has an attribute __str__
2337
var klass = arg.__class__ || $B.get_class(arg)
2343
// if not better than object.__str__, try __repr__
2344
(arg.__class__ && arg.__class__ !== _b_.object &&
2345
method.$infos && method.$infos.__func__ === _b_.object.__str__)){
2346
var method = $B.$getattr(klass, "__repr__")
2352
if($B.debug > 1){console.log(err)}
2353
console.log("Warning - no method __str__ or __repr__, " +
2354
"default to toString", arg)
2361
if(cls === undefined){
2362
throw _b_.TypeError.$factory("str.__new__(): not enough arguments")
2386
var args = [],
2387
pos = 0
2388
if(arguments.length > 0){
2389
var args = [arguments[0].valueOf()],
2390
pos = 1
2391
for(var i = 1, len = arguments.length; i < len; i++){
2392
args[pos++] = arguments[i]
2409
// Function to parse the 2nd argument of format()
2410
$B.parse_format_spec = function(spec){
2414
var pos = 0,
2415
aligns = "<>=^",
2416
digits = "0123456789",
2417
types = "bcdeEfFgGnosxX%",
2419
if(align_pos != -1){
2420
if(spec.charAt(1) && aligns.indexOf(spec.charAt(1)) != -1){
2421
// If the second char is also an alignment specifier, the
2422
// first char is the fill value
2423
this.fill = spec.charAt(0)
2424
this.align = spec.charAt(1)
2425
pos = 2
2426
}else{
2427
// The first character defines alignment : fill defaults to ' '
2442
if(car == "+" || car == "-" || car == " "){
2443
this.sign = car
2444
pos++
2445
car = spec.charAt(pos)
2460
if(this.width === undefined){
2461
this.width = car
2462
}else{
2463
this.width += car
2464
}
2471
if(this.width === undefined && car == "{"){
2472
// Width is determined by a parameter
2473
var end_param_pos = spec.substr(pos).search("}")
2474
this.width = spec.substring(pos, end_param_pos)
2475
console.log("width", "[" + this.width + "]")
2476
pos += end_param_pos + 1
2477
}
2478
if(car == ","){
2479
this.comma = true
2480
pos++
2481
car = spec.charAt(pos)
2482
}
2483
if(car == "."){
2484
if(digits.indexOf(spec.charAt(pos + 1)) == -1){
2485
throw _b_.ValueError.$factory(
2486
"Missing precision in format spec")
2488
this.precision = spec.charAt(pos + 1)
2489
pos += 2
2490
car = spec.charAt(pos)
2491
while(car && digits.indexOf(car) > -1){
2498
if(car && types.indexOf(car) > -1){
2499
this.type = car
2500
pos++
2501
car = spec.charAt(pos)
2502
}
2503
if(pos !== spec.length){
2509
return (this.fill === undefined ? "" : _b_.str.$factory(this.fill)) +
2510
(this.align || "") +
2511
(this.sign || "") +
2512
(this.alternate ? "#" : "") +
2513
(this.sign_aware ? "0" : "") +
2514
(this.width || "") +
2515
(this.comma ? "," : "") +
2516
(this.precision ? "." + this.precision : "") +
2517
(this.type || "")
2522
if(fmt.width && s.length < fmt.width){
2523
var fill = fmt.fill || " ",
2524
align = fmt.align || "<",
2525
missing = fmt.width - s.length
2527
case "<":
2528
return s + fill.repeat(missing)
2529
case ">":
2530
return fill.repeat(missing) + s
2531
case "=":
2532
if("+-".indexOf(s.charAt(0)) > -1){
2533
return s.charAt(0) + fill.repeat(missing) + s.substr(1)
2537
case "^":
2538
var left = parseInt(missing / 2)
2539
return fill.repeat(left) + s + fill.repeat(missing - left)
2552
function fstring_error(msg, pos){
2553
error = Error(msg)
2554
error.position = pos
2555
throw error
2556
}
2557
2558
$B.parse_fstring = function(string){
2559
// Parse a f-string
2560
var elts = [],
2561
pos = 0,
2623
}else if(ctype == "debug"){
2624
// after the equal sign, whitespace are ignored and the only
2625
// valid characters are } and :
2626
while(string.charAt(i) == " "){i++}
2627
if(string.charAt(i) == "}"){
2628
// end of debug expression
2629
elts.push(current)
2630
ctype = null
2631
current = ""
2632
pos = i + 1
2633
}
2634
}else{
2635
// End of expression is the } matching the opening {
2636
// There may be nested braces
2637
var i = pos,
2638
nb_braces = 1,
2651
if(current.expression == ""){
2652
fstring_error("f-string: empty expression not allowed",
2653
pos)
2654
}
2664
// backslash is not allowed in expressions
2665
throw Error("f-string expression part cannot include a" +
2666
" backslash")
2673
throw Error("f-string: invalid conversion character:" +
2674
" expected 's', 'r', or 'a'")
2675
}else{
2689
if(string.substr(i, 3) == '"""'){
2690
var end = string.indexOf('"""', i + 3)
2691
if(end == -1){
2693
}else{
2694
var trs = string.substring(i, end + 3)
2695
trs = trs.replace("\n", "\\n\\")
2714
var ce = current.expression,
2715
last_char = ce.charAt(ce.length - 1),
2716
last_char_re = ('()'.indexOf(last_char) > -1 ? "\\" : "") + last_char
2721
"=!<>:".search(last_char_re) > -1){
2722
current.expression += car //+ string.charAt(i + 1)
2723
i += 1
2724
}else{
2725
// add debug string
2726
tail = car
2727
while(string.charAt(i + 1).match(/\s/)){
2728
tail += string.charAt(i + 1)
2729
i++
2730
}
2731
elts.push(current.expression + tail)
2732
// remove trailing whitespace from expression
2733
while(ce.match(/\s$/)){
2734
ce = ce.substr(0, ce.length - 1)
2735
}
2736
current.expression = ce
2737
ctype = "debug"
2738
i++
2739
}
2755
if(i >= 0x10000 && i <= 0x10FFFF){
2756
var code = (i - 0x10000)
2757
return String.fromCodePoint(0xD800 | (code >> 10)) +
2758
String.fromCodePoint(0xDC00 | (code & 0x3FF))
2759
}else{
2760
return String.fromCodePoint(i)
2761
}
2762
}
2765
if(c.length == 1){
2766
return c.charCodeAt(0)
2767
}
2768
var code = 0x10000
2769
code += (c.charCodeAt(0) & 0x03FF) << 10
2770
code += (c.charCodeAt(1) & 0x03FF)
2771
return code
2772
}
2773