Permalink
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
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Dec 20, 2018
Dec 20, 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
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 19, 2018
Mar 19, 2018
Mar 27, 2019
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Apr 2, 2019
Mar 19, 2018
Aug 31, 2017
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Aug 31, 2017
Apr 16, 2019
Mar 19, 2018
Apr 16, 2019
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Nov 12, 2018
Nov 12, 2018
Nov 12, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Apr 2, 2019
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
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 19, 2018
Mar 19, 2018
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 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Feb 27, 2020
Feb 27, 2020
Feb 27, 2020
Aug 31, 2017
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Nov 14, 2018
Jan 26, 2020
Mar 19, 2018
Mar 19, 2018
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 19, 2018
Mar 19, 2018
Nov 12, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Mar 19, 2018
Jun 29, 2017
Jun 29, 2017
Jun 29, 2017
Oct 13, 2019
Jun 29, 2017
Jul 10, 2017
Jul 10, 2017
Mar 19, 2018
Mar 19, 2018
Oct 13, 2019
Dec 27, 2019
Oct 13, 2019
Newer
100644
2426 lines (2227 sloc)
77.4 KB
21
if($.start === null || $.start === _b_.None){$.start = 0}
22
else if($.start < 0){
23
$.start += $.self.length
24
$.start = Math.max(0, $.start)
25
}
26
if($.end === null || $.end === _b_.None){$.end = $.self.length}
27
else if($.end < 0){
28
$.end += $.self.length
29
$.end = Math.max(0, $.end)
30
}
32
if(! isinstance($.start, _b_.int) || ! isinstance($.end, _b_.int)){
33
throw _b_.TypeError.$factory("slice indices must be integers " +
34
"or None or have an __index__ method")
35
}
52
if(!(typeof other === "string")){
53
try{return getattr(other, "__radd__")(self)}
54
catch(err){
55
throw _b_.TypeError.$factory("Can't convert " +
61
str.__contains__ = function(self, item){
62
if(!(typeof item == "string")){
63
throw _b_.TypeError.$factory("'in <string>' requires " +
64
"string as left operand, not " + item.__class__)
65
}
67
if(nbcar == 0) {return true} // a string contains the empty string
68
if(self.length == 0){return nbcar == 0}
69
for(var i = 0, len = self.length; i < len; i++){
70
if(self.substr(i, nbcar) == item){return true}
79
// __dir__must be assigned explicitely because attribute resolution for
80
// builtin classes doesn't use __mro__
84
if(other === undefined){ // compare object "self" to class "str"
85
return self === str
95
if(fmt.type && fmt.type != "s"){
96
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
104
if(fmt.sign !== undefined){
105
throw _b_.ValueError.$factory(
106
"Sign not allowed in string format specifier")
116
if(arg < 0) {pos += self.length}
117
if(pos >= 0 && pos < self.length){return self.charAt(pos)}
118
throw _b_.IndexError.$factory("string index out of range")
119
}
120
if(isinstance(arg, slice)) {
121
var s = _b_.slice.$conv_for_seq(arg, self.length),
122
start = s.start,
123
stop = s.stop,
124
step = s.step
125
var res = "",
127
if(step > 0){
128
if(stop <= start){return ""}
129
for(var i = start; i < stop; i += step){res += self.charAt(i)}
131
if(stop >= start){return ''}
132
for(var i = start; i > stop; i += step){res += self.charAt(i)}
140
var prefix = 2,
141
suffix = 3,
142
mask = (2 ** 32 - 1)
143
function fnv(p){
144
if(p.length == 0){
145
return 0
146
}
148
var x = prefix
149
x = (x ^ (p.charCodeAt(0) << 7)) & mask
150
for(var i = 0, len = p.length; i < len; i++){
151
x = ((1000003 * x) ^ p.charCodeAt(i)) & mask
152
}
153
x = (x ^ p.length) & mask
154
x = (x ^ suffix) & mask
212
// left adjusted
213
return s + get_char_array(padding - s.length, flags.pad_char)
214
}
215
}
216
224
if(val.__class__ === $B.long_int){
225
s = $B.long_int.to_base(val, 10)
226
}else{
227
s = val.toString()
229
if(s[0] === "-"){
230
return "-" + get_char_array(precision - s.length + 1, "0") + s.slice(1)
241
if(val === Infinity){
242
val = "inf"
243
}else if(val === -Infinity){
244
val = "-inf"
245
}else{
246
val = "nan"
268
var str_format = function(val, flags) {
269
// string format supports left and right padding
270
flags.pad_char = " " // even if 0 padding is defined, don't use it
276
if(val.__class__ === $B.long_int){
277
val = $B.long_int.to_base(val, 10)
278
}else{
279
val = parseInt(val)
297
var repr_format = function(val, flags) {
298
flags.pad_char = " " // even if 0 padding is defined, don't use it
299
return format_padding(repr(val), flags)
300
}
302
var ascii_format = function(val, flags) {
303
flags.pad_char = " " // even if 0 padding is defined, don't use it
304
return format_padding(ascii(val), flags)
305
}
317
flags.precision = parseInt(flags.precision, 10)
318
validate_precision(flags.precision)
319
}
320
return parseFloat(val)
321
}
324
var trailing_zeros = /(.*?)(0+)([eE].*)/,
325
leading_zeros = /\.(0*)/,
326
trailing_dot = /\.$/
328
var validate_precision = function(precision) {
329
// force precision to limits of javascript
334
var floating_point_format = function(val, upper, flags){
335
val = _float_helper(val, flags),
336
v = val.toString(),
337
v_len = v.length,
338
dot_idx = v.indexOf('.')
339
if(dot_idx < 0){dot_idx = v_len}
340
if(val < 1 && val > -1){
341
var zeros = leading_zeros.exec(v),
342
numzeros
343
if(zeros){
348
if(numzeros >= 4){
349
val = format_sign(val, flags) + format_float_precision(val, upper,
350
flags, _floating_g_exp_helper)
351
if(!flags.alternate){
364
return format_padding(format_sign(val, flags) +
365
format_float_precision(val, upper, flags,
366
function(val, precision) {
367
return val.toFixed(min(precision, v_len - dot_idx) +
368
numzeros)
369
}),
370
flags
371
)
372
}
373
374
if(dot_idx > flags.precision){
375
val = format_sign(val, flags) + format_float_precision(val, upper,
376
flags, _floating_g_exp_helper)
377
if(! flags.alternate){
389
return format_padding(format_sign(val, flags) +
390
format_float_precision(val, upper, flags,
391
function(val, precision) {
392
if(!flags.decimal_point){
393
precision = min(v_len - 1, 6)
394
}else if (precision > v_len){
395
if(! flags.alternate){
396
precision = v_len
397
}
399
if(precision < dot_idx){
400
precision = dot_idx
401
}
402
return val.toFixed(precision - dot_idx)
403
}),
404
flags
405
)
408
var _floating_g_exp_helper = function(val, precision, flags, upper){
409
if(precision){--precision}
412
var e_idx = val.lastIndexOf("e")
413
if(e_idx > val.length - 4){
414
val = val.substring(0, e_idx + 2) + "0" + val.substring(e_idx + 2)
417
return val
418
}
419
420
// fF
421
var floating_point_decimal_format = function(val, upper, flags) {
422
val = _float_helper(val, flags)
423
return format_padding(format_sign(val, flags) +
424
format_float_precision(val, upper, flags,
425
function(val, precision, flags) {
426
val = val.toFixed(precision)
427
if(precision === 0 && flags.alternate){
428
val += '.'
429
}
430
return val
431
}),
432
flags
433
)
434
}
435
436
var _floating_exp_helper = function(val, precision, flags, upper) {
437
val = val.toExponential(precision)
438
// pad exponent to two digits
451
return format_padding(format_sign(val, flags) +
452
format_float_precision(val, upper, flags, _floating_exp_helper), flags)
478
if(flags.alternate){
479
if(ret.charAt(0) === "-"){
480
if(upper){ret = "-0X" + ret.slice(1)}
481
else{ret = "-0x" + ret.slice(1)}
482
}else{
483
if(upper){ret = "0X" + ret}
484
else{ret = "0x" + ret}
494
if(val.__class__ === $B.long_int){
495
ret = $B.long_int.to_base(8)
496
}else{
497
ret = parseInt(val)
498
ret = ret.toString(8)
514
if(flags.alternate){
515
if(ret.charAt(0) === "-"){ret = "-0o" + ret.slice(1)}
516
else{ret = "0o" + ret}
521
function series_of_bytes(val, flags){
522
if(val.__class__ && val.__class__.$buffer_protocol){
523
var it = _b_.iter(val),
524
ints = []
525
while(true){
526
try{
527
ints.push(_b_.next(it))
528
}catch(err){
529
if(err.__class__ === _b_.StopIteration){
530
var b = _b_.bytes.$factory(ints)
531
return format_padding(_b_.bytes.decode(b, "ascii"), flags)
532
}
533
throw err
534
}
535
}
536
}else{
537
try{
538
bytes_obj = $B.$getattr(val, "__bytes__")
539
return format_padding(_b_.bytes.decode(bytes_obj), flags)
540
}catch(err){
541
if(err.__class__ === _b_.AttributeError){
542
throw _b_.TypeError.$factory("%b does not accept '" +
543
$B.class_name(val) + "'")
544
}
545
throw err
546
}
547
}
548
}
549
551
if(isinstance(val, str) && val.length == 1){
552
return val
553
}else if(isinstance(val, bytes) && val.source.length == 1){
554
val = val.source[0]
555
}else{
556
try{
557
val = _b_.int.$factory(val) // yes, floats are valid (they are cast to int)
558
}catch (err){
559
throw _b_.TypeError.$factory("%c requires int or char")
560
}
565
var num_flag = function(c, flags){
566
if(c === "0" && ! flags.padding && ! flags.decimal_point && ! flags.left){
567
flags.pad_char = "0"
573
flags.precision = (flags.precision || "") + c
574
}
575
}
576
577
var decimal_point_flag = function(val, flags) {
579
// can only have one decimal point
580
throw new UnsupportedChar()
581
}
582
flags.decimal_point = true
583
}
584
585
var neg_flag = function(val, flags){
586
flags.pad_char = " " // overrides '0' flag
604
"s": str_format,
605
"d": num_format,
606
"i": num_format,
607
"u": num_format,
608
"o": octal_format,
609
"r": repr_format,
610
"a": ascii_format,
611
"g": function(val, flags){
612
return floating_point_format(val, false, flags)
613
},
614
"G": function(val, flags){return floating_point_format(val, true, flags)},
615
"f": function(val, flags){
616
return floating_point_decimal_format(val, false, flags)
617
},
618
"F": function(val, flags){
619
return floating_point_decimal_format(val, true, flags)
620
},
621
"e": function(val, flags){
622
return floating_point_exponential_format(val, false, flags)
623
},
624
"E": function(val, flags){
625
return floating_point_exponential_format(val, true, flags)
626
},
627
"x": function(val, flags){return signed_hex_format(val, false, flags)},
628
"X": function(val, flags){return signed_hex_format(val, true, flags)},
629
"c": single_char_format,
630
"0": function(val, flags){return num_flag("0", flags)},
631
"1": function(val, flags){return num_flag("1", flags)},
632
"2": function(val, flags){return num_flag("2", flags)},
633
"3": function(val, flags){return num_flag("3", flags)},
634
"4": function(val, flags){return num_flag("4", flags)},
635
"5": function(val, flags){return num_flag("5", flags)},
636
"6": function(val, flags){return num_flag("6", flags)},
637
"7": function(val, flags){return num_flag("7", flags)},
638
"8": function(val, flags){return num_flag("8", flags)},
639
"9": function(val, flags){return num_flag("9", flags)},
640
"-": neg_flag,
641
" ": space_flag,
642
"+": sign_flag,
643
".": decimal_point_flag,
644
"#": alternate_flag
645
}
646
647
// exception thrown when an unsupported char is encountered in legacy format
694
if(self === undefined){
695
throw _b_.TypeError.$factory(
696
"not enough arguments for format string")
723
throw _b_.ValueError.$factory(
724
"unsupported format character '" + invalid_char +
725
"' (0x" + invalid_char.charCodeAt(0).toString(16) +
726
") at index " + newpos)
727
}else if(err.name === "NotANumber"){
728
var try_char = s[newpos],
729
cls = self.__class__
730
if(!cls){
731
if(typeof(self) === "string"){
732
cls = "str"
733
}else{
739
throw _b_.TypeError.$factory("%" + try_char +
740
" format: a number is required, not " + cls)
741
}else{
773
}while(pos < length)
774
775
if(argpos !== null){
776
if(args.length > argpos){
777
throw _b_.TypeError.$factory(
778
"not enough arguments for format string")
779
}else if(args.length < argpos){
780
throw _b_.TypeError.$factory(
781
"not all arguments converted during string formatting")
783
}else if(nbph == 0){
784
throw _b_.TypeError.$factory(
785
"not all arguments converted during string formatting")
793
var $ = $B.args("__mul__", 2, {self: null, other: null},
794
["self", "other"], arguments, {}, null, null)
795
if(! isinstance($.other, _b_.int)){throw _b_.TypeError.$factory(
796
"Can't multiply sequence by non-int of type '" +
808
res = self.replace(/\\/g, "\\\\")
809
// special cases
810
res = res.replace(new RegExp("\u0007", "g"), "\\x07").
811
replace(new RegExp("\b", "g"), "\\x08").
813
replace(new RegExp("\f", "g"), "\\x0c").
814
replace(new RegExp("\n", "g"), "\\n").
815
replace(new RegExp("\r", "g"), "\\r").
816
replace(new RegExp("\t", "g"), "\\t")
818
if(res.search('"') == -1 && res.search("'") == -1){
819
return "'" + res + "'"
820
}else if(self.search('"') == -1){
821
return '"' + res + '"'
822
}
823
var qesc = new RegExp("'", "g") // to escape single quote
824
res = "'" + res.replace(qesc, "\\'") + "'"
828
str.__setitem__ = function(self, attr, value){
829
throw _b_.TypeError.$factory(
830
"'str' object does not support item assignment")
832
var combining = []
833
for(var cp = 0x300; cp <= 0x36F; cp++){
834
combining.push(String.fromCharCode(cp))
835
}
836
var combining_re = new RegExp("(" + combining.join("|") + ")")
848
$comp_func += "" // source code
849
var $comps = {">": "gt", ">=": "ge", "<": "lt", "<=": "le"}
858
var $notimplemented = function(self, other){
859
throw NotImplementedError.$factory(
860
"OPERATOR not implemented for class str")
863
str.capitalize = function(self){
864
var $ = $B.args("capitalize", 1, {self}, ["self"],
865
arguments, {}, null, null)
866
if(self.length == 0){return ""}
867
return self.charAt(0).toUpperCase() + self.substr(1)
868
}
869
870
str.casefold = function(self){
871
var $ = $B.args("casefold", 1, {self}, ["self"],
872
arguments, {}, null, null),
873
res = "",
874
char,
875
cf
876
for(var i = 0, len = self.length; i < len; i++){
877
char = self.charCodeAt(i)
878
cf = $B.unicode_casefold[char]
879
if(cf){
880
cf.forEach(function(cp){
881
res += String.fromCharCode(cp)
882
})
883
}else{
884
res += self.charAt(i).toLowerCase()
885
}
886
}
887
return res
888
}
890
str.center = function(){
891
var $ = $B.args("center", 3, {self: null, width: null, fillchar: null},
892
["self", "width", "fillchar"],
893
arguments, {fillchar:" "}, null, null),
894
self = $.self
906
var $ = $B.args("count", 4, {self:null, sub:null, start:null, stop:null},
907
["self", "sub", "start", "stop"], arguments, {start:null, stop:null},
915
if($.stop !== null){_slice = _b_.slice.$factory($.start, $.stop)}
916
else{_slice = _b_.slice.$factory($.start, $.self.length)}
921
if($.sub.length == 0){
922
if($.start == $.self.length){return 1}
923
else if(substr.length == 0){return 0}
924
return substr.length + 1
926
var n = 0,
927
pos = 0
928
while(pos < substr.length){
929
pos = substr.indexOf($.sub, pos)
930
if(pos >= 0){n++; pos += $.sub.length}
931
else{break}
936
str.encode = function(){
937
var $ = $B.args("encode", 3, {self: null, encoding: null, errors: null},
938
["self", "encoding", "errors"], arguments,
939
{encoding: "utf-8", errors: "strict"}, null, null)
940
if($.encoding == "rot13" || $.encoding == "rot_13"){
945
if(("a" <= char && char <= "m") || ("A" <= char && char <= "M")){
946
res += String.fromCharCode(String.charCodeAt(char) + 13)
947
}else if(("m" < char && char <= "z") ||
948
("M" < char && char <= "Z")){
949
res += String.fromCharCode(String.charCodeAt(char) - 13)
958
// Return True if the string ends with the specified suffix, otherwise
959
// return False. suffix can also be a tuple of suffixes to look for.
960
// With optional start, test beginning at that position. With optional
964
["self", "suffix", "start", "end"],
965
arguments, {start: 0, end: null}, null, null)
972
var s = $.self.substring($.start, $.end)
973
for(var i = 0, len = suffixes.length; i < len; i++){
977
if(suffix.length <= s.length &&
978
s.substr(s.length - suffix.length) == suffix){return true}
984
var $ = $B.args("expandtabs", 2, {self: null, tabsize: null},
985
["self", "tabsize"], arguments, {tabsize: 8}, null, null)
986
var s = $B.$GetInt($.tabsize),
987
col = 0,
988
pos = 0,
989
res = ""
990
if(s == 1){return self.replace(/\t/g," ")}
991
while(pos < self.length){
999
res += car
1000
col = 0
1001
break
1002
default:
1003
res += car
1004
col++
1005
break
1006
}
1007
pos++
1008
}
1014
// Return the lowest index in the string where substring sub is found,
1015
// such that sub is contained in the slice s[start:end]. Optional
1016
// arguments start and end are interpreted as in slice notation.
1019
{self: null, sub: null, start: null, end: null},
1020
["self", "sub", "start", "end"],
1021
arguments, {start: 0, end: null}, null, null)
1025
if(!isinstance($.start, _b_.int)||!isinstance($.end, _b_.int)){
1026
throw _b_.TypeError.$factory("slice indices must be " +
1027
"integers or None or have an __index__ method")}
1028
// Can't use string.substring(start, end) because if end < start,
1029
// Javascript transforms it into substring(end, start)...
1030
var s = ""
1031
for(var i = $.start; i < $.end; i++){
1032
s += $.self.charAt(i)
1033
}
1035
if($.sub.length == 0 && $.start == $.self.length){return $.self.length}
1036
if(s.length + $.sub.length == 0){return -1}
1038
var last_search = s.length - $.sub.length
1039
for(var i = 0; i <= last_search; i++){
1040
if(s.substr(i, $.sub.length) == $.sub){return $.start + i}
1051
// a.x[z]!r:...
1052
// the object has attributes :
1053
// - name : "a"
1054
// - name_ext : [".x", "[z]"]
1055
// - conv : r
1056
// - spec : rest of string after :
1064
// No : in the string : it only contains a name
1065
name = fmt_string
1066
}else{
1067
// name is before the first ":"
1068
// spec (the format specification) is after
1069
name = elts[0]
1073
var elts = name.split("!")
1074
if(elts.length > 1){
1075
name = elts[0]
1076
conv = elts[1] // conversion flag
1080
// "name' may be a subscription or attribute
1081
// Put these "extensions" in the list "name_ext"
1082
function name_repl(match){
1083
name_ext.push(match)
1085
}
1086
var name_ext_re = /\.[_a-zA-Z][_a-zA-Z0-9]*|\[[_a-zA-Z][_a-zA-Z0-9]*\]|\[[0-9]+\]/g
1087
name = name.replace(name_ext_re, name_repl)
1088
}
1095
// Parse self to detect formatting instructions
1096
// Create a list "parts" made of sections of the string :
1097
// - elements of even rank are literal text
1098
// - elements of odd rank are "format objects", built from the
1099
// format strings in self (of the form {...})
1110
text += "{"
1111
pos += 2
1112
}else if(car == "}" && self.charAt(pos + 1) == "}"){
1119
// Store current literal text
1120
parts.push(text)
1121
1122
// Search the end of the format string, ie the } closing the
1123
// opening {. Since the string can contain other pairs {} for
1124
// nested formatting, an integer nb is incremented for each { and
1125
// decremented for each } ; the end of the format string is
1126
// reached when nb == 0
1127
var end = pos + 1,
1128
nb = 1
1129
while(end < _len){
1130
if(self.charAt(end) == "{"){nb++; end++}
1131
else if(self.charAt(end) == "}"){
1132
nb--; end++
1133
if(nb == 0){
1138
var fmt_obj = $B.parse_format(fmt_string)
1139
fmt_obj.raw_name = fmt_obj.name
1140
fmt_obj.raw_spec = fmt_obj.spec
1175
return parts
1176
}
1177
1178
str.format = function(self) {
1179
var $ = $B.args("format", 1, {self: null}, ["self"],
1180
arguments, {}, "$args", "$kw")
1181
1182
var parts = $B.split_format($.self)
1183
1194
1195
if(fmt.spec !== undefined){
1196
// "spec" may contain "nested replacement fields"
1197
// In this case, evaluate them using the positional
1198
// or keyword arguments passed to format()
1199
function replace_nested(name, key){
1200
if(/\d+/.exec(key)){
1201
// If key is numeric, search in positional
1202
// arguments
1203
return _b_.tuple.__getitem__($.$args,
1204
parseInt(key))
1205
}else{
1206
// Else try in keyword arguments
1207
return _b_.dict.__getitem__($.$kw, key)
1208
}
1209
}
1210
fmt.spec = fmt.spec.replace(/\{(.*?)\}/g,
1211
replace_nested)
1212
}
1214
// Numerical reference : use positional arguments
1215
var pos = parseInt(fmt.name),
1225
// Attribute
1226
value = _b_.getattr(value, ext.substr(1))
1227
}else{
1228
// Subscription
1231
if(key.charAt(0).search(/\d/) > -1){key = parseInt(key)}
1232
value = _b_.getattr(value, "__getitem__")(key)
1236
// If the conversion flag is set, first call a function to convert
1237
// the value
1238
if(fmt.conv == "a"){value = _b_.ascii(value)}
1239
else if(fmt.conv == "r"){value = _b_.repr(value)}
1240
else if(fmt.conv == "s"){value = _b_.str.$factory(value)}
1254
throw NotImplementedError.$factory(
1255
"function format_map not implemented yet")
1266
/* Return true if the string is empty or all characters in the string are
1267
ASCII, false otherwise. ASCII characters have code points in the range
1268
U+0000-U+007F. */
1269
for(var i = 0, len = self.length; i < len; i++){
1270
if(self.charCodeAt(i) > 127){return false}
1271
}
1272
return true
1273
}
1274
1275
str.isalnum = function(self){
1276
/* Return true if all characters in the string are alphanumeric and there
1277
is at least one character, false otherwise. A character c is alphanumeric
1278
if one of the following returns True: c.isalpha(), c.isdecimal(),
1279
c.isdigit(), or c.isnumeric(). */
1280
var $ = $B.args("isalnum", 1, {self: null}, ["self"],
1281
arguments, {}, null, null),
1282
char
1283
for(var i = 0, len = self.length; i < len; i++){
1284
char = self.charCodeAt(i)
1285
if(unicode_tables.Ll[char] ||
1286
unicode_tables.Lu[char] ||
1287
unicode_tables.Lm[char] ||
1288
unicode_tables.Lt[char] ||
1289
unicode_tables.Lo[char] ||
1290
unicode_tables.Nd[char] ||
1291
unicode_tables.digits[char] ||
1292
unicode_tables.numeric[char]){
1293
continue
1294
}
1295
return false
1296
}
1297
return true
1298
}
1299
1300
str.isalpha = function(self){
1301
/* Return true if all characters in the string are alphabetic and there is
1302
at least one character, false otherwise. Alphabetic characters are those
1303
characters defined in the Unicode character database as "Letter", i.e.,
1304
those with general category property being one of "Lm", "Lt", "Lu", "Ll",
1305
or "Lo". */
1306
var $ = $B.args("isalpha", 1, {self: null}, ["self"],
1307
arguments, {}, null, null),
1308
char
1309
for(var i = 0, len = self.length; i < len; i++){
1310
char = self.charCodeAt(i)
1311
if(unicode_tables.Ll[char] ||
1312
unicode_tables.Lu[char] ||
1313
unicode_tables.Lm[char] ||
1314
unicode_tables.Lt[char] ||
1315
unicode_tables.Lo[char]){
1316
continue
1317
}
1318
return false
1319
}
1320
return true
1321
}
1322
1323
str.isdecimal = function(self){
1324
/* Return true if all characters in the string are decimal characters and
1325
there is at least one character, false otherwise. Decimal characters are
1326
those that can be used to form numbers in base 10, e.g. U+0660,
1327
ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in
1328
the Unicode General Category "Nd". */
1329
var $ = $B.args("isdecimal", 1, {self: null}, ["self"],
1330
arguments, {}, null, null),
1331
char
1332
for(var i = 0, len = self.length; i < len; i++){
1333
char = self.charCodeAt(i)
1334
if(! unicode_tables.Nd[char]){
1335
return false
1336
}
1337
}
1338
return self.length > 0
1339
}
1340
1341
str.isdigit = function(self){
1342
/* Return true if all characters in the string are digits and there is at
1343
least one character, false otherwise. */
1344
var $ = $B.args("isdigit", 1, {self: null}, ["self"],
1345
arguments, {}, null, null),
1346
char
1347
for(var i = 0, len = self.length; i < len; i++){
1348
char = self.charCodeAt(i)
1349
if(! unicode_tables.digits[char]){
1350
return false
1351
}
1352
}
1353
return self.length > 0
1354
}
1355
1356
str.isidentifier = function(self){
1357
/* Return true if the string is a valid identifier according to the
1358
language definition. */
1359
var $ = $B.args("isidentifier", 1, {self: null}, ["self"],
1360
arguments, {}, null, null),
1361
char
1362
if(self.length == 0){return false}
1363
else if(unicode_tables.XID_Start[self.charCodeAt(0)] === undefined){
1364
return false
1365
}else{
1366
for(var i = 1, len = self.length; i < len; i++){
1367
if(unicode_tables.XID_Continue[self.charCodeAt(i)] === undefined){
1368
return false
1369
}
1370
}
1371
}
1372
return true
1373
}
1374
1375
str.islower = function(self){
1376
/* Return true if all cased characters 4 in the string are lowercase and
1377
there is at least one cased character, false otherwise. */
1378
var $ = $B.args("islower", 1, {self: null}, ["self"],
1379
arguments, {}, null, null),
1380
has_cased = false,
1381
char
1382
1383
for(var i = 0, len = self.length; i < len; i++){
1384
char = self.charCodeAt(i)
1385
if(unicode_tables.Ll[char]){has_cased = true; continue}
1386
else if(unicode_tables.Lu[char] || unicode_tables.Lt[char]){
1387
return false
1388
}
1389
}
1390
return has_cased
1391
}
1392
1393
str.isnumeric = function(self){
1394
/* Return true if all characters in the string are numeric characters, and
1395
there is at least one character, false otherwise. Numeric characters
1396
include digit characters, and all characters that have the Unicode numeric
1397
value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric
1398
characters are those with the property value Numeric_Type=Digit,
1399
Numeric_Type=Decimal or Numeric_Type=Numeric.*/
1400
var $ = $B.args("isnumeric", 1, {self: null}, ["self"],
1401
arguments, {}, null, null)
1402
for(var i = 0, len = self.length; i < len; i++){
1403
if(! unicode_tables.numeric[self.charCodeAt(i)]){
1404
return false
1405
}
1406
}
1407
return self.length > 0
1408
}
1409
1410
var printable,
1411
printable_gc = ['Cc', 'Cf', 'Co', 'Cs','Zl', 'Zp', 'Zs']
1412
1413
str.isprintable = function(self){
1414
/* Return true if all characters in the string are printable or the string
1415
is empty, false otherwise. Nonprintable characters are those characters
1416
defined in the Unicode character database as "Other" or "Separator",
1417
excepting the ASCII space (0x20) which is considered printable. */
1418
1419
// Set printable if not set yet
1420
if(printable === undefined){
1421
for(var i = 0; i < printable_gc.length; i++){
1422
var table = unicode_tables[printable_gc[i]]
1423
for(var cp in table){
1424
printable[cp] = true
1425
}
1426
}
1427
printable[32] = true
1428
}
1429
1430
var $ = $B.args("isprintable", 1, {self: null}, ["self"],
1431
arguments, {}, null, null),
1432
char,
1433
flag
1434
for(var i = 0, len = self.length; i < len; i++){
1435
char = self.charCodeAt(i)
1436
if(! printable[char]){
1437
return false
1438
}
1439
}
1440
return true
1441
}
1442
1443
str.isspace = function(self){
1444
/* Return true if there are only whitespace characters in the string and
1445
there is at least one character, false otherwise.
1446
1447
A character is whitespace if in the Unicode character database, either its
1448
general category is Zs ("Separator, space"), or its bidirectional class is
1449
one of WS, B, or S.*/
1450
var $ = $B.args("isspace", 1, {self: null}, ["self"],
1451
arguments, {}, null, null),
1452
char
1453
for(var i = 0, len = self.length; i < len; i++){
1454
char = self.charCodeAt(i)
1455
if(! unicode_tables.Zs[char] &&
1456
$B.unicode_bidi_whitespace.indexOf(char) == -1){
1457
return false
1458
}
1459
}
1460
return self.length > 0
1461
}
1462
1463
str.istitle = function(self){
1464
/* Return true if the string is a titlecased string and there is at least
1465
one character, for example uppercase characters may only follow uncased
1466
characters and lowercase characters only cased ones. Return false
1467
otherwise. */
1468
var $ = $B.args("istitle", 1, {self: null}, ["self"],
1469
arguments, {}, null, null)
1470
return self.length > 0 && str.title(self) == self
1471
}
1472
1473
str.isupper = function(self){
1474
/* Return true if all cased characters 4 in the string are lowercase and
1475
there is at least one cased character, false otherwise. */
1476
var $ = $B.args("islower", 1, {self: null}, ["self"],
1477
arguments, {}, null, null),
1478
has_cased = false,
1479
char
1480
1481
for(var i = 0, len = self.length; i < len; i++){
1482
char = self.charCodeAt(i)
1483
if(unicode_tables.Lu[char]){has_cased = true; continue}
1484
else if(unicode_tables.Ll[char] || unicode_tables.Lt[char]){
1502
if(! isinstance(obj2, str)){throw _b_.TypeError.$factory(
1503
"sequence item " + count + ": expected str instance, " +
1517
var $ = $B.args("ljust", 3, {self: null, width: null, fillchar:null},
1518
["self", "width", "fillchar"],
1519
arguments, {fillchar: " "}, null, null)
1525
str.lower = function(self){
1526
var $ = $B.args("lower", 1, {self: null}, ["self"],
1527
arguments, {}, null, null)
1528
return self.toLowerCase()
1529
}
1530
1532
var $ = $B.args("lstrip", 2, {self: null, chars: null}, ["self", "chars"],
1533
arguments, {chars:_b_.None}, null, null)
1534
if($.chars === _b_.None){return $.self.trimLeft()}
1535
for(var i = 0; i < $.self.length; i++){
1536
if($.chars.indexOf($.self.charAt(i)) === -1){
1537
return $.self.substring(i)
1545
var $ = $B.args("maketrans", 3, {x: null, y: null, z: null},
1546
["x", "y", "z"], arguments, {y: null, z: null}, null, null)
1551
// If there is only one argument, it must be a dictionary mapping
1552
// Unicode ordinals (integers) or characters (strings of length 1) to
1553
// Unicode ordinals, strings (of arbitrary lengths) or None. Character
1555
if(! _b_.isinstance($.x, _b_.dict)){
1556
throw _b_.TypeError.$factory(
1557
"maketrans only argument must be a dict")
1560
for(var i = 0, len = items.length; i < len; i++){
1561
var k = items[i][0],
1562
v = items[i][1]
1563
if(! _b_.isinstance(k, _b_.int)){
1564
if(_b_.isinstance(k, _b_.str) && k.length == 1){
1565
k = _b_.ord(k)
1566
}else{throw _b_.TypeError.$factory("dictionary key " + k +
1569
if(v !== _b_.None && ! _b_.isinstance(v, [_b_.int, _b_.str])){
1570
throw _b_.TypeError.$factory("dictionary value " + v +
1578
// and in the resulting dictionary, each character in x will be mapped
1579
// to the character at the same position in y
1582
}else if($.x.length !== $.y.length){
1583
throw _b_.TypeError.$factory(
1584
"maketrans arguments must be strings or same length")
1590
if(! _b_.isinstance($.z, _b_.str)){
1591
throw _b_.TypeError.$factory(
1592
"maketrans third argument must be a string")
1614
var $ = $B.args("partition", 2, {self: null, sep: null}, ["self", "sep"],
1615
arguments, {}, null, null)
1620
return _b_.tuple.$factory([$.self.substring(0, i), $.sep,
1621
$.self.substring(i + $.sep.length)])
1622
}
1623
1624
function $re_escape(str){
1625
var specials = "[.*+?|()$^"
1626
for(var i = 0, len = specials.length; i < len; i++){
1627
var re = new RegExp("\\"+specials.charAt(i), "g")
1628
str = str.replace(re, "\\"+specials.charAt(i))
1629
}
1630
return str
1637
var $ = $B.args("replace", 4,
1638
{self: null, old: null, $$new: null, count: null},
1639
["self", "old", "$$new", "count"],
1640
arguments, {count: -1}, null, null),
1641
count = $.count,
1642
self = $.self,
1643
old = $.old,
1644
_new = $.$$new
1651
"' object cannot be interpreted as an integer")
1652
}else if(isinstance(count, _b_.float)){
1653
throw _b_.TypeError.$factory("integer argument expected, got float")
1654
}
1655
if(count == 0){return self}
1656
if(count.__class__ == $B.long_int){count = parseInt(count.value)}
1657
if(old == ""){
1658
if(_new == ""){return self}
1659
if(self == ""){return _new}
1660
var elts = self.split("")
1661
if(count > -1 && elts.length >= count){
1662
var rest = elts.slice(count).join("")
1663
return _new + elts.slice(0, count).join(_new) + rest
1664
}else{return _new + elts.join(_new) + _new}
1679
if(count < 0){count = res.length}
1680
while(count > 0){
1681
pos = res.indexOf(old, pos)
1682
if(pos < 0){break}
1683
res = res.substr(0, pos) + _new + res.substr(pos + old.length)
1684
pos = pos + _new.length
1685
count--
1691
// Return the highest index in the string where substring sub is found,
1692
// such that sub is contained within s[start:end]. Optional arguments
1694
if(arguments.length == 2 && typeof substr == "string"){
1695
return self.lastIndexOf(substr)
1696
}
1698
{self: null, sub: null, start: null, end: null},
1699
["self", "sub", "start", "end"],
1700
arguments, {start: 0, end: null}, null, null)
1712
for(var i = $.end - sublen; i >= $.start; i--){
1713
if($.self.substr(i, sublen) == $.sub){return i}
1720
var res = str.rfind.apply(null, arguments)
1721
if(res == -1){throw _b_.ValueError.$factory("substring not found")}
1726
var $ = $B.args("rjust",3,
1727
{self: null, width: null, fillchar: null},
1728
["self", "width", "fillchar"],
1729
arguments, {fillchar: " "}, null, null)
1737
var $ = $B.args("rpartition", 2, {self: null, sep: null}, ["self", "sep"],
1738
arguments, {}, null, null)
1742
var items = str.partition(self, sep).reverse()
1743
for(var i = 0; i < items.length; i++){
1744
items[i] = items[i].split("").reverse().join("")
1750
var $ = $B.args("rsplit", 3, {self: null, sep: null, maxsplit: null},
1751
["self", "sep", "maxsplit"], arguments,
1752
{sep: _b_.None, maxsplit: -1}, null, null),
1753
sep = $.sep
1756
var rev_str = reverse($.self),
1757
rev_sep = sep === _b_.None ? sep : reverse($.sep),
1768
str.rstrip = function(self, x){
1769
var $ = $B.args("rstrip", 2, {self: null, chars: null}, ["self", "chars"],
1770
arguments, {chars: _b_.None}, null, null)
1771
if($.chars === _b_.None){return $.self.trimRight()}
1773
if($.chars.indexOf($.self.charAt(j)) == -1){
1774
return $.self.substring(0, j + 1)
1781
var $ = $B.args("split", 3, {self: null, sep: null, maxsplit: null},
1782
["self", "sep", "maxsplit"], arguments,
1783
{sep: _b_.None, maxsplit: -1}, null, null),
1784
sep = $.sep,
1785
maxsplit = $.maxsplit,
1786
self = $.self,
1787
pos = 0
1788
if(maxsplit.__class__ === $B.long_int){maxsplit = parseInt(maxsplit.value)}
1789
if(sep == ""){throw _b_.ValueError.$factory("empty separator")}
1790
if(sep === _b_.None){
1792
while(pos < self.length && self.charAt(pos).search(/\s/) > -1){pos++}
1793
if(pos === self.length - 1){return [self]}
1794
var name = ""
1796
if(self.charAt(pos).search(/\s/) == -1){
1797
if(name == ""){name = self.charAt(pos)}
1798
else{name += self.charAt(pos)}
1818
var res = [],
1819
s = "",
1820
seplen = sep.length
1821
if(maxsplit == 0){return [self]}
1822
while(pos < self.length){
1823
if(self.substr(pos, seplen) == sep){
1841
str.splitlines = function(self) {
1842
var $ = $B.args('splitlines', 2, {self: null, keepends: null},
1843
['self','keepends'], arguments, {keepends: false},
1844
null, null)
1845
if(!_b_.isinstance($.keepends,[_b_.bool, _b_.int])){
1846
throw _b_.TypeError('integer argument expected, got '+
1849
var keepends = _b_.int.$factory($.keepends),
1850
res = [],
1851
self = $.self,
1852
start = 0,
1853
pos = 0
1854
if(!self.length){
1857
while (pos < self.length) {
1858
if(self.substr(pos, 2) == '\r\n'){
1859
res.push(self.slice(start, keepends ? pos + 2 : pos))
1860
start = pos = pos+2
1861
}else if(self[pos] == '\r' || self[pos] == '\n'){
1862
res.push(self.slice(start, keepends ? pos+1 : pos))
1863
start = pos = pos+1
1864
}else{
1865
pos++
1866
}
1867
}
1868
if(start < self.length){
1869
res.push(self.slice(start))
1870
}
1871
return res
1875
// Return True if string starts with the prefix, otherwise return False.
1876
// prefix can also be a tuple of prefixes to look for. With optional
1877
// start, test string beginning at that position. With optional end,
1879
var $ = $B.args("startswith", 4,
1880
{self: null, prefix: null, start: null, end: null},
1881
["self", "prefix", "start", "end"],
1882
arguments, {start: 0, end: null}, null, null)
1889
var s = $.self.substring($.start, $.end)
1890
for(var i = 0, len = prefixes.length; i < len; i++){
1901
var $ = $B.args("strip", 2, {self: null, chars: null}, ["self", "chars"],
1902
arguments, {chars: _b_.None}, null, null)
1903
if($.chars === _b_.None){return $.self.trim()}
1904
for(var i = 0; i < $.self.length; i++){
1905
if($.chars.indexOf($.self.charAt(i)) == -1){
1906
break
1909
for(var j = $.self.length - 1; j >= i; j--){
1910
if($.chars.indexOf($.self.charAt(j)) == -1){
1911
break
1917
str.swapcase = function(self){
1918
var $ = $B.args("swapcase", 1, {self}, ["self"],
1919
arguments, {}, null, null),
1920
res = "",
1921
char
1922
1923
for(var i = 0, len = self.length; i < len; i++){
1924
char = self.charCodeAt(i)
1925
if(unicode_tables.Ll[char]){
1926
res += self.charAt(i).toUpperCase()
1927
}else if(unicode_tables.Lu[char]){
1928
res += self.charAt(i).toLowerCase()
1929
}else{
1930
res += self.charAt(i)
1931
}
1932
}
1933
return res
1934
}
1935
1936
str.title = function(self){
1937
var $ = $B.args("title", 1, {self}, ["self"],
1938
arguments, {}, null, null),
1939
state,
1940
char,
1941
res = ""
1942
for(var i = 0, len = self.length; i < len; i++){
1943
char = self.charCodeAt(i)
1944
if(unicode_tables.Ll[char]){
1945
if(! state){
1946
res += self.charAt(i).toUpperCase()
1947
state = "word"
1948
}else{
1949
res += self.charAt(i)
1950
}
1951
}else if(unicode_tables.Lu[char] || unicode_tables.Lt[char]){
1952
res += state ? self.charAt(i).toLowerCase() : self.charAt(i)
1953
state = "word"
1954
}else{
1955
state = null
1956
res += self.charAt(i)
1957
}
1958
}
1959
return res
1960
}
1961
1964
getitem = $B.$getattr(table, "__getitem__")
1965
for(var i = 0, len = self.length; i < len; i++){
1966
try{
1967
var repl = getitem(self.charCodeAt(i))
1968
if(repl !== _b_.None){
1969
if(typeof repl == "string"){
1970
res.push(repl)
1971
}else if(typeof repl == "number"){
1972
res.push(String.fromCharCode(repl))
1973
}
1982
str.upper = function(self){
1983
var $ = $B.args("upper", 1, {self: null}, ["self"],
1984
arguments, {}, null, null)
1985
return self.toUpperCase()
1986
}
1987
1990
["self", "width"], arguments, {}, null, null)
1991
if($.width <= self.length){return self}
1993
case "+":
1994
case "-":
1995
return self.charAt(0) +
1996
"0".repeat($.width - self.length) + self.substr(1)
2004
if(arg === undefined){
2005
throw _b_.TypeError.$factory("str() argument is undefined")
2006
}
2007
if(encoding !== undefined){
2008
// Arguments may be passed as keywords (cf. issue #1060)
2009
var $ = $B.args("str", 3, {arg: null, encoding: null, errors: null},
2010
["arg", "encoding", "errors"], arguments,
2011
{encoding: "utf-8", errors: "strict"}, null, null),
2012
encoding = $.encoding,
2013
errors = $.errors
2014
}
2025
// class or its subclasses, but the attribute __str__ of the
2026
// class metaclass (usually "type") or its subclasses (usually
2027
// "object")
2028
// The metaclass is the attribute __class__ of the class dictionary
2033
if(arg.__class__ && arg.__class__ === _b_.bytes &&
2034
encoding !== undefined){
2035
// str(bytes, encoding, errors) is equal to
2036
// bytes.decode(encoding, errors)
2039
// Implicit invocation of __str__ uses method __str__ on the class,
2040
// even if arg has an attribute __str__
2041
var klass = arg.__class__ || $B.get_class(arg)
2042
var method = $B.$getattr(klass , "__str__", null)
2043
if(method === null ||
2044
// if not better than object.__str__, try __repr__
2045
(arg.__class__ && arg.__class__ !== _b_.object &&
2046
method.$infos && method.$infos.__func__ === _b_.object.__str__)){
2047
var method = $B.$getattr(klass, "__repr__")
2053
if($B.debug > 1){console.log(err)}
2054
console.log("Warning - no method __str__ or __repr__, " +
2055
"default to toString", arg)
2062
if(cls === undefined){
2063
throw _b_.TypeError.$factory("str.__new__(): not enough arguments")
2087
var args = [],
2088
pos = 0
2089
if(arguments.length > 0){
2090
var args = [arguments[0].valueOf()],
2091
pos = 1
2092
for(var i = 1, len = arguments.length; i < len; i++){
2093
args[pos++] = arguments[i]
2109
// Function to parse the 2nd argument of format()
2110
$B.parse_format_spec = function(spec){
2113
var pos = 0,
2114
aligns = "<>=^",
2115
digits = "0123456789",
2116
types = "bcdeEfFgGnosxX%",
2118
if(align_pos != -1){
2119
if(spec.charAt(1) && aligns.indexOf(spec.charAt(1)) != -1){
2120
// If the second char is also an alignment specifier, the
2121
// first char is the fill value
2122
this.fill = spec.charAt(0)
2123
this.align = spec.charAt(1)
2124
pos = 2
2125
}else{
2126
// The first character defines alignment : fill defaults to ' '
2141
if(car == "+" || car == "-" || car == " "){
2142
this.sign = car
2143
pos++
2144
car = spec.charAt(pos)
2146
if(car == "#"){this.alternate = true; pos++; car = spec.charAt(pos)}
2147
if(car == "0"){
2156
while(car && digits.indexOf(car) > -1){
2157
if(this.width === undefined){this.width = car}
2158
else{this.width += car}
2159
pos++
2160
car = spec.charAt(pos)
2163
if(this.width === undefined && car == "{"){
2164
// Width is determined by a parameter
2165
var end_param_pos = spec.substr(pos).search("}")
2166
this.width = spec.substring(pos, end_param_pos)
2167
console.log("width", "[" + this.width + "]")
2168
pos += end_param_pos + 1
2169
}
2170
if(car == ","){this.comma = true; pos++; car = spec.charAt(pos)}
2171
if(car == "."){
2172
if(digits.indexOf(spec.charAt(pos + 1)) == -1){
2173
throw _b_.ValueError.$factory(
2174
"Missing precision in format spec")
2176
this.precision = spec.charAt(pos + 1)
2177
pos += 2
2178
car = spec.charAt(pos)
2179
while(car && digits.indexOf(car) > -1){
2186
if(car && types.indexOf(car) > -1){
2187
this.type = car
2188
pos++
2189
car = spec.charAt(pos)
2190
}
2191
if(pos !== spec.length){
2197
return (this.fill === undefined ? "" : _b_.str.$factory(this.fill)) +
2198
(this.align || "") +
2199
(this.sign || "") +
2200
(this.alternate ? "#" : "") +
2201
(this.sign_aware ? "0" : "") +
2202
(this.width || "") +
2203
(this.comma ? "," : "") +
2204
(this.precision ? "." + this.precision : "") +
2205
(this.type || "")
2210
if(fmt.width && s.length < fmt.width){
2211
var fill = fmt.fill || " ",
2212
align = fmt.align || "<",
2213
missing = fmt.width - s.length
2215
case "<":
2216
return s + fill.repeat(missing)
2217
case ">":
2218
return fill.repeat(missing) + s
2219
case "=":
2220
if("+-".indexOf(s.charAt(0)) > -1){
2221
return s.charAt(0) + fill.repeat(missing) + s.substr(1)
2225
case "^":
2226
var left = parseInt(missing / 2)
2227
return fill.repeat(left) + s + fill.repeat(missing - left)
2240
$B.parse_fstring = function(string){
2241
// Parse a f-string
2242
var elts = [],
2243
pos = 0,
2267
}else{
2268
throw Error(" f-string: single '}' is not allowed")
2269
}
2270
}else{
2292
current += car
2293
i += 2
2294
}else{
2295
throw Error(" f-string: single '}' is not allowed")
2296
}
2297
}else{
2298
current += car
2299
i++
2300
}
2301
}
2303
}else if(ctype == "debug"){
2304
// after the equal sign, whitespace are ignored and the only
2305
// valid characters are } and :
2306
while(string.charAt(i) == " "){i++}
2307
if(string.charAt(i) == "}"){
2308
// end of debug expression
2309
elts.push(current)
2310
ctype = null
2311
current = ""
2312
pos = i + 1
2313
}
2314
}else{
2315
// End of expression is the } matching the opening {
2316
// There may be nested braces
2317
var i = pos,
2318
nb_braces = 1,
2340
// backslash is not allowed in expressions
2341
throw Error("f-string expression part cannot include a" +
2342
" backslash")
2349
throw Error("f-string: invalid conversion character:" +
2350
" expected 's', 'r', or 'a'")
2351
}else{
2365
if(string.substr(i, 3) == '"""'){
2366
var end = string.indexOf('"""', i + 3)
2367
if(end == -1){
2368
throw Error("f-string: unterminated string")
2369
}else{
2370
var trs = string.substring(i, end + 3)
2371
trs = trs.replace("\n", "\\n\\")
2376
var end = string.indexOf('"', i + 1)
2377
if(end == -1){
2378
throw Error("f-string: unterminated string")
2379
}else{
2380
current.expression += string.substring(i, end + 1)
2381
i = end + 1
2388
}else if(car == "="){
2389
// might be a "debug expression", eg f"{x=}"
2390
var ce = current.expression
2391
if(ce.length == 0 ||
2392
string.charAt(i + 1) == "=" ||
2393
"=!<>:".search(ce.charAt(ce.length - 1)) > -1){
2396
}else{
2397
// add debug string
2398
tail = car
2399
while(string.charAt(i + 1).match(/\s/)){
2400
tail += string.charAt(i + 1)
2401
i++
2402
}
2403
elts.push(current.expression + tail)
2404
// remove trailing whitespace from expression
2405
while(ce.match(/\s$/)){
2406
ce = ce.substr(0, ce.length - 1)
2407
}
2408
current.expression = ce
2409
ctype = "debug"
2410
i++
2411
}