Source

pypy / pypy / rpython / rstr.py

The branch 'remove-globals-in-jit' does not exist.
Antonio Cuni a0d2385 
Carl Friedrich B… f0f8ccf 
Antonio Cuni f06c2ef 
Boris Feigin cca62c8 
Antonio Cuni f06c2ef 
Antonio Cuni aad4c9d 
Michael Hudson-D… b9c0fe6 

Antonio Cuni dcb5ef9 

Boris Feigin cca62c8 
Maciej Fijalkows… d594fdd 
Armin Rigo 4ac21ef 
Antonio Cuni dcb5ef9 

Antonio Cuni aad4c9d 

Antonio Cuni 5ca4f66 



Antonio Cuni 8f530e0 


Antonio Cuni aac3247 
Antonio Cuni aad4c9d 




Amaury Forgeot d… 10e2fbb 



Antonio Cuni aad4c9d 
Antonio Cuni dcb5ef9 
Antonio Cuni 8f530e0 




Antonio Cuni dcb5ef9 
Alex Gaynor 26fac49 











Antonio Cuni dcb5ef9 
Maciej Fijalkows… 7ebbd95 
Antonio Cuni dcb5ef9 

Maciej Fijalkows… 7ebbd95 
Antonio Cuni f06c2ef 

Antonio Cuni 5ca4f66 



Amaury Forgeot d… 10e2fbb 


Antonio Cuni f06c2ef 
Maciej Fijalkows… 7ebbd95 





Antonio Cuni f06c2ef 




Amaury Forgeot d… 10e2fbb 



Antonio Cuni f06c2ef 

Amaury Forgeot d… 10e2fbb 



Boris Feigin cca62c8 

Niklaus Haldiman… 85dfc23 
Boris Feigin cca62c8 


Maciej Fijalkows… d594fdd 


Alex Gaynor 6724e4b 
Maciej Fijalkows… d594fdd 


Boris Feigin cca62c8 

Antonio Cuni dcb5ef9 
Boris Feigin cca62c8 




Antonio Cuni dcb5ef9 
Boris Feigin cca62c8 



Niklaus Haldiman… 123c41f 
Boris Feigin cca62c8 
Maciej Fijalkows… 7ebbd95 


Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 

Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Armin Rigo 538e28f 
Antonio Cuni 20b9423 
Armin Rigo 538e28f 
Antonio Cuni 20b9423 
Maciej Fijalkows… d594fdd 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 



Maciej Fijalkows… 7ebbd95 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 

Niklaus Haldiman… 123c41f 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Alex Gaynor 740fc1d 








Boris Feigin cca62c8 
Alex Gaynor 740fc1d 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Alex Gaynor 740fc1d 








Boris Feigin cca62c8 
Alex Gaynor 740fc1d 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Maciej Fijalkows… 7ebbd95 





Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Maciej Fijalkows… 7ebbd95 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 







Armin Rigo 4ac21ef 
Boris Feigin cca62c8 

Antonio Cuni 20b9423 
Boris Feigin cca62c8 





Carl Friedrich B… 32ae2cd 
Maciej Fijalkows… 7ebbd95 

Carl Friedrich B… 32ae2cd 



Maciej Fijalkows… 7ebbd95 
Carl Friedrich B… 32ae2cd 








Armin Rigo 4ac21ef 
Carl Friedrich B… 32ae2cd 





Niklaus Haldiman… 123c41f 
Maciej Fijalkows… 7ebbd95 

Antonio Cuni dcb5ef9 
Seo Sanghyeon 0a65c20 

Maciej Fijalkows… 773fac2 
Antonio Cuni 20b9423 
Seo Sanghyeon 0a65c20 






Niklaus Haldiman… 123c41f 
Maciej Fijalkows… 7ebbd95 
Boris Feigin cca62c8 

Antonio Cuni 20b9423 
Alex Gaynor 6724e4b 
Niklaus Haldiman… 123c41f 
Maciej Fijalkows… 7ebbd95 
Boris Feigin cca62c8 

Antonio Cuni 20b9423 
Antonio Cuni 3122528 
Alex Gaynor e35b788 





Alex Gaynor ff3a902 





Antonio Cuni 3122528 




Niklaus Haldiman… 123c41f 
Boris Feigin cca62c8 
Maciej Fijalkows… 7ebbd95 
Boris Feigin cca62c8 
Maciej Fijalkows… 7ebbd95 
Boris Feigin cca62c8 
Niklaus Haldiman… 0207885 
Boris Feigin cca62c8 
Maciej Fijalkows… 7ebbd95 
Antonio Cuni 3122528 

Boris Feigin cca62c8 
Maciej Fijalkows… 7ebbd95 
Antonio Cuni 20b9423 
Benjamin Peterso… c13b0d0 




Boris Feigin cca62c8 



Maciej Fijalkows… 7ebbd95 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 



Armin Rigo 4ac21ef 













Antonio Cuni 20b9423 
Maciej Fijalkows… 7ebbd95 
Amaury Forgeot d… 12df7ba 




Antonio Cuni 2910a27 




Boris Feigin cca62c8 
Amaury Forgeot d… 12df7ba 
Boris Feigin cca62c8 
Amaury Forgeot d… 7be96de 














Antonio Cuni 20b9423 
Maciej Fijalkows… 7ebbd95 
Antonio Cuni dcb5ef9 
Boris Feigin cca62c8 
Maciej Fijalkows… 7ebbd95 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Maciej Fijalkows… 7ebbd95 
Boris Feigin cca62c8 



Antonio Cuni 20b9423 
Boris Feigin cca62c8 



Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Maciej Fijalkows… 7ebbd95 

Armin Rigo b39ccd2 

Maciej Fijalkows… 7ebbd95 


Carl Friedrich B… 35ae678 
Carl Friedrich B… 4d6a6a5 

Maciej Fijalkows… 7ebbd95 


Maciej Fijalkows… 33301a2 








Maciej Fijalkows… 7ebbd95 
Carl Friedrich B… 2c292ac 



Maciej Fijalkows… 7ebbd95 
Carl Friedrich B… 2c292ac 



Antonio Cuni 18ef63b 
Antonio Cuni 5ca4f66 
Antonio Cuni 18ef63b 
Carl Friedrich B… 2c292ac 

Maciej Fijalkows… 7ebbd95 
Samuele Pedroni 0771572 

Maciej Fijalkows… 7ebbd95 
Samuele Pedroni 0771572 



Boris Feigin cca62c8 
Armin Rigo c0b0d50 



Boris Feigin cca62c8 
Maciej Fijalkows… 7ebbd95 

Carl Friedrich B… 2c292ac 


Armin Rigo e8b9fe3 

Armin Rigo 590ea6a 


Maciej Fijalkows… 7ebbd95 
Carl Friedrich B… 2c292ac 



Antonio Cuni c3807d8 
Antonio Cuni 5ca4f66 
Antonio Cuni c3807d8 
Carl Friedrich B… 2c292ac 


Armin Rigo de4b244 







Niklaus Haldiman… 123c41f 
Samuele Pedroni 2d5cb5d 
Maciej Fijalkows… d594fdd 
Boris Feigin cca62c8 
Samuele Pedroni 2d5cb5d 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 

Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Samuele Pedroni 2d5cb5d 



Boris Feigin cca62c8 

Samuele Pedroni 2d5cb5d 






Alex Gaynor 6724e4b 










Boris Feigin cca62c8 
Armin Rigo 676af77 
Boris Feigin cca62c8 
Armin Rigo 676af77 
Maciej Fijalkows… 7ebbd95 
Armin Rigo 676af77 



Boris Feigin cca62c8 
Niklaus Haldiman… 123c41f 
Antonio Cuni 20b9423 
Maciej Fijalkows… 7ebbd95 

Samuele Pedroni a7685a0 
Maciej Fijalkows… 7ebbd95 

Antonio Cuni 20b9423 
Boris Feigin cca62c8 

Antonio Cuni 20b9423 
Maciej Fijalkows… 7ebbd95 
Antonio Cuni 20b9423 
Alex Gaynor 6724e4b 
Antonio Cuni 20b9423 
Maciej Fijalkows… 7ebbd95 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 

Antonio Cuni 20b9423 
Maciej Fijalkows… 7ebbd95 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 


Antonio Cuni 20b9423 
Maciej Fijalkows… 7ebbd95 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 


Antonio Cuni 20b9423 
Maciej Fijalkows… 7ebbd95 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 


Antonio Cuni 20b9423 
Maciej Fijalkows… 7ebbd95 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 


Carl Friedrich B… d14270b 




Armin Rigo 7cedc12 
Carl Friedrich B… d14270b 



Maciej Fijalkows… 7ebbd95 

Antonio Cuni 20b9423 
Maciej Fijalkows… 7ebbd95 


Armin Rigo 7cedc12 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Antonio Cuni dcb5ef9 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 



Armin Rigo ff146d7 


Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Armin Rigo 751669a 










Antonio Cuni 02b40b8 

Boris Feigin cca62c8 
Maciej Fijalkows… 7ebbd95 

Boris Feigin cca62c8 






Alex Gaynor 6724e4b 
Boris Feigin cca62c8 

Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Armin Rigo 538e28f 

Boris Feigin cca62c8 







Maciej Fijalkows… 7ebbd95 

Boris Feigin cca62c8 


Maciej Fijalkows… 7ebbd95 

Boris Feigin cca62c8 



Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Maciej Fijalkows… 7ebbd95 

Alex Gaynor 6724e4b 
Antonio Cuni 20b9423 
Maciej Fijalkows… 7ebbd95 

Antonio Cuni 20b9423 
Boris Feigin cca62c8 

Maciej Fijalkows… 7ebbd95 

Antonio Cuni 20b9423 
Maciej Fijalkows… 7ebbd95 

Antonio Cuni 20b9423 
Boris Feigin cca62c8 

Antonio Cuni dcb5ef9 
Boris Feigin cca62c8 









Antonio Cuni dcb5ef9 

Boris Feigin cca62c8 

Antonio Cuni dcb5ef9 
Boris Feigin cca62c8 

Armin Rigo c90e6a7 

Boris Feigin cca62c8 




Alex Gaynor 6724e4b 
Boris Feigin cca62c8 

Antonio Cuni 20b9423 
Boris Feigin cca62c8 
Armin Rigo 538e28f 

Boris Feigin cca62c8 







Antonio Cuni dcb5ef9 

Boris Feigin cca62c8 


Armin Rigo c90e6a7 


Boris Feigin cca62c8 









Antonio Cuni dcb5ef9 

Boris Feigin cca62c8 





Maciej Fijalkows… 7ebbd95 

Boris Feigin cca62c8 
Antonio Cuni dcb5ef9 
Maciej Fijalkows… 7ebbd95 

Antonio Cuni 20b9423 
Boris Feigin cca62c8 

Antonio Cuni dcb5ef9 
Boris Feigin cca62c8 
Antonio Cuni dcb5ef9 

Boris Feigin cca62c8 
Antonio Cuni 20b9423 
Boris Feigin cca62c8 

Armin Rigo c90e6a7 



Boris Feigin cca62c8 




Niklaus Haldiman… 85dfc23 

Boris Feigin cca62c8 
Maciej Fijalkows… 7ebbd95 
Boris Feigin cca62c8 
Niklaus Haldiman… 85dfc23 
Boris Feigin cca62c8 




Niklaus Haldiman… 85dfc23 
Boris Feigin cca62c8 
Antonio Cuni a0d2385 













Alex Gaynor e35b788 

Antonio Cuni a0d2385 
Alex Gaynor e35b788 






Antonio Cuni a0d2385 
Alex Gaynor ff3a902 










Alex Gaynor e35b788 







Antonio Cuni a0d2385 




















Maciej Fijalkows… 7ebbd95 
Antonio Cuni a0d2385 


Antonio Cuni b5e5638 









Antonio Cuni a0d2385 








Antonio Cuni c98b3e0 
Antonio Cuni a0d2385 




Antonio Cuni c98b3e0 
Antonio Cuni a0d2385 




Antonio Cuni c98b3e0 
Antonio Cuni a0d2385 







Antonio Cuni c98b3e0 
Antonio Cuni 60171e9 
























Samuele Pedroni 0771572 


Amaury Forgeot d… d7a6c72 
Samuele Pedroni 0771572 
Samuele Pedroni bdac81f 
Samuele Pedroni 0771572 
















Amaury Forgeot d… 1b64a0d 
Armin Rigo 4ac21ef 































  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
from pypy.tool.staticmethods import StaticMethods
from pypy.tool.pairtype import pairtype, pair
from pypy.tool.sourcetools import func_with_new_name
from pypy.annotation import model as annmodel
from pypy.rlib import jit
from pypy.rlib.nonconst import NonConstant
from pypy.rpython.error import TyperError
from pypy.rpython.rmodel import IntegerRepr, IteratorRepr
from pypy.rpython.rmodel import inputconst, Repr
from pypy.rpython.rtuple import AbstractTupleRepr
from pypy.rpython import rint
from pypy.rpython.lltypesystem.lltype import Signed, Bool, Void, UniChar,\
     cast_primitive, typeOf

class AbstractStringRepr(Repr):

    def __init__(self, *args):
        Repr.__init__(self, *args)
        self.rstr_decode_utf_8 = None

    def ensure_ll_decode_utf8(self):
        from pypy.rlib.runicode import str_decode_utf_8_impl
        self.rstr_decode_utf_8 = func_with_new_name(str_decode_utf_8_impl,
                                                    'rstr_decode_utf_8_impl')

    @jit.elidable
    def ll_decode_utf8(self, llvalue):
        from pypy.rpython.annlowlevel import hlstr
        value = hlstr(llvalue)
        assert value is not None
        univalue, _ = self.rstr_decode_utf_8(
            value, len(value), 'strict', final=False,
            errorhandler=self.ll_raise_unicode_exception_decode,
            allow_surrogates=False)
        return self.ll.llunicode(univalue)

    def ll_raise_unicode_exception_decode(self, errors, encoding, msg, s,
                                       startingpos, endingpos):
        raise UnicodeDecodeError(encoding, s, startingpos, endingpos, msg)
    

class AbstractCharRepr(AbstractStringRepr):
    def rtype_method_lower(self, hop):
        char_repr = hop.args_r[0].char_repr
        v_chr, = hop.inputargs(char_repr)
        hop.exception_cannot_occur()
        return hop.gendirectcall(self.ll.ll_lower_char, v_chr)

    def rtype_method_upper(self, hop):
        char_repr = hop.args_r[0].char_repr
        v_chr, = hop.inputargs(char_repr)
        hop.exception_cannot_occur()
        return hop.gendirectcall(self.ll.ll_upper_char, v_chr)


class AbstractUniCharRepr(AbstractStringRepr):
    pass

class AbstractUnicodeRepr(AbstractStringRepr):

    def __init__(self, *args):
        AbstractStringRepr.__init__(self, *args)
        self.runicode_encode_utf_8 = None

    def ensure_ll_encode_utf8(self):
        from pypy.rlib.runicode import unicode_encode_utf_8_impl
        self.runicode_encode_utf_8 = func_with_new_name(
            unicode_encode_utf_8_impl, 'runicode_encode_utf_8')

    def rtype_method_upper(self, hop):
        raise TypeError("Cannot do toupper on unicode string")

    def rtype_method_lower(self, hop):
        raise TypeError("Cannot do tolower on unicode string")

    @jit.elidable
    def ll_encode_utf8(self, ll_s):
        from pypy.rpython.annlowlevel import hlunicode
        s = hlunicode(ll_s)
        assert s is not None
        bytes = self.runicode_encode_utf_8(
            s, len(s), 'strict',
            errorhandler=self.ll_raise_unicode_exception_decode,
            allow_surrogates=False)
        return self.ll.llstr(bytes)

    def ll_raise_unicode_exception_encode(self, errors, encoding, msg, u,
                                          startingpos, endingpos):
        raise UnicodeEncodeError(encoding, u, startingpos, endingpos, msg)
    
class __extend__(annmodel.SomeString):
    def rtyper_makerepr(self, rtyper):
        return rtyper.type_system.rstr.string_repr
    def rtyper_makekey(self):
        return self.__class__,

class __extend__(annmodel.SomeUnicodeString):
    def rtyper_makerepr(self, rtyper):
        return rtyper.type_system.rstr.unicode_repr

    def rtyper_makekey(self):
        return self.__class__,

class __extend__(annmodel.SomeChar):
    def rtyper_makerepr(self, rtyper):
        return rtyper.type_system.rstr.char_repr
    def rtyper_makekey(self):
        return self.__class__,

class __extend__(annmodel.SomeUnicodeCodePoint):
    def rtyper_makerepr(self, rtyper):
        return rtyper.type_system.rstr.unichar_repr
    def rtyper_makekey(self):
        return self.__class__,


class __extend__(AbstractStringRepr):

    def _str_reprs(self, hop):
        return hop.args_r[0].repr, hop.args_r[1].repr

    def get_ll_eq_function(self):
        return self.ll.ll_streq

    def get_ll_hash_function(self):
        return self.ll.ll_strhash

    def get_ll_fasthash_function(self):
        return self.ll.ll_strfasthash

    def rtype_len(self, hop):
        string_repr = self.repr
        v_str, = hop.inputargs(string_repr)
        return hop.gendirectcall(self.ll.ll_strlen, v_str)

    def rtype_is_true(self, hop):
        s_str = hop.args_s[0]
        if s_str.can_be_None:
            string_repr = hop.args_r[0].repr
            v_str, = hop.inputargs(string_repr)
            return hop.gendirectcall(self.ll.ll_str_is_true, v_str)
        else:
            # defaults to checking the length
            return super(AbstractStringRepr, self).rtype_is_true(hop)

    def rtype_method_startswith(self, hop):
        str1_repr = hop.args_r[0].repr
        str2_repr = hop.args_r[1]
        v_str = hop.inputarg(str1_repr, arg=0)
        if str2_repr == str2_repr.char_repr:
            v_value = hop.inputarg(str2_repr.char_repr, arg=1)
            fn = self.ll.ll_startswith_char
        else:
            v_value = hop.inputarg(str2_repr, arg=1)
            fn = self.ll.ll_startswith
        hop.exception_cannot_occur()
        return hop.gendirectcall(fn, v_str, v_value)

    def rtype_method_endswith(self, hop):
        str1_repr = hop.args_r[0].repr
        str2_repr = hop.args_r[1]
        v_str = hop.inputarg(str1_repr, arg=0)
        if str2_repr == str2_repr.char_repr:
            v_value = hop.inputarg(str2_repr.char_repr, arg=1)
            fn = self.ll.ll_endswith_char
        else:
            v_value = hop.inputarg(str2_repr, arg=1)
            fn = self.ll.ll_endswith
        hop.exception_cannot_occur()
        return hop.gendirectcall(fn, v_str, v_value)

    def rtype_method_find(self, hop, reverse=False):
        # XXX binaryop
        string_repr = hop.args_r[0].repr
        char_repr = hop.args_r[0].char_repr
        v_str = hop.inputarg(string_repr, arg=0)
        if hop.args_r[1] == char_repr:
            v_value = hop.inputarg(char_repr, arg=1)
            llfn = reverse and self.ll.ll_rfind_char or self.ll.ll_find_char
        else:
            v_value = hop.inputarg(string_repr, arg=1)
            llfn = reverse and self.ll.ll_rfind or self.ll.ll_find
        if hop.nb_args > 2:
            v_start = hop.inputarg(Signed, arg=2)
            if not hop.args_s[2].nonneg:
                raise TyperError("str.find() start must be proven non-negative")
        else:
            v_start = hop.inputconst(Signed, 0)
        if hop.nb_args > 3:
            v_end = hop.inputarg(Signed, arg=3)
            if not hop.args_s[3].nonneg:
                raise TyperError("str.find() end must be proven non-negative")
        else:
            v_end = hop.gendirectcall(self.ll.ll_strlen, v_str)
        hop.exception_cannot_occur()
        return hop.gendirectcall(llfn, v_str, v_value, v_start, v_end)

    def rtype_method_rfind(self, hop):
        return self.rtype_method_find(hop, reverse=True)

    def rtype_method_count(self, hop):
        rstr = hop.args_r[0].repr
        v_str = hop.inputarg(rstr.repr, arg=0)
        if hop.args_r[1] == rstr.char_repr:
            v_value = hop.inputarg(rstr.char_repr, arg=1)
            llfn = self.ll.ll_count_char
        else:
            v_value = hop.inputarg(rstr.repr, arg=1)
            llfn = self.ll.ll_count
        if hop.nb_args > 2:
            v_start = hop.inputarg(Signed, arg=2)
            if not hop.args_s[2].nonneg:
                raise TyperError("str.count() start must be proven non-negative")
        else:
            v_start = hop.inputconst(Signed, 0)
        if hop.nb_args > 3:
            v_end = hop.inputarg(Signed, arg=3)
            if not hop.args_s[3].nonneg:
                raise TyperError("str.count() end must be proven non-negative")
        else:
            v_end = hop.gendirectcall(self.ll.ll_strlen, v_str)
        hop.exception_cannot_occur()
        return hop.gendirectcall(llfn, v_str, v_value, v_start, v_end)

    def rtype_method_strip(self, hop, left=True, right=True):
        rstr = hop.args_r[0].repr
        v_str = hop.inputarg(rstr.repr, arg=0)
        v_char = hop.inputarg(rstr.char_repr, arg=1)
        v_left = hop.inputconst(Bool, left)
        v_right = hop.inputconst(Bool, right)
        hop.exception_is_here()
        return hop.gendirectcall(self.ll.ll_strip, v_str, v_char, v_left, v_right)

    def rtype_method_lstrip(self, hop):
        return self.rtype_method_strip(hop, left=True, right=False)

    def rtype_method_rstrip(self, hop):
        return self.rtype_method_strip(hop, left=False, right=True)

    def rtype_method_upper(self, hop):
        string_repr = hop.args_r[0].repr
        v_str, = hop.inputargs(string_repr)
        hop.exception_cannot_occur()
        return hop.gendirectcall(self.ll.ll_upper, v_str)

    def rtype_method_lower(self, hop):
        string_repr = hop.args_r[0].repr
        v_str, = hop.inputargs(string_repr)
        hop.exception_cannot_occur()
        return hop.gendirectcall(self.ll.ll_lower, v_str)

    def rtype_method_isdigit(self, hop):
        string_repr = hop.args_r[0].repr
        [v_str] = hop.inputargs(string_repr)
        hop.exception_cannot_occur()
        return hop.gendirectcall(self.ll.ll_isdigit, v_str)

    def rtype_method_isalpha(self, hop):
        string_repr = hop.args_r[0].repr
        [v_str] = hop.inputargs(string_repr)
        hop.exception_cannot_occur()
        return hop.gendirectcall(self.ll.ll_isalpha, v_str)

    def _list_length_items(self, hop, v_lst, LIST):
        """Return two Variables containing the length and items of a
        list. Need to be overriden because it is typesystem-specific."""
        raise NotImplementedError

    def rtype_method_join(self, hop):
        hop.exception_cannot_occur()
        rstr = hop.args_r[0]
        if hop.s_result.is_constant():
            return inputconst(rstr.repr, hop.s_result.const)
        r_lst = hop.args_r[1]
        if not isinstance(r_lst, hop.rtyper.type_system.rlist.BaseListRepr):
            raise TyperError("string.join of non-list: %r" % r_lst)
        v_str, v_lst = hop.inputargs(rstr.repr, r_lst)
        v_length, v_items = self._list_length_items(hop, v_lst, r_lst.lowleveltype)

        if hop.args_s[0].is_constant() and hop.args_s[0].const == '':
            if r_lst.item_repr == rstr.repr:
                llfn = self.ll.ll_join_strs
            elif (r_lst.item_repr == hop.rtyper.type_system.rstr.char_repr or
                  r_lst.item_repr == hop.rtyper.type_system.rstr.unichar_repr):
                v_tp = hop.inputconst(Void, self.lowleveltype)
                return hop.gendirectcall(self.ll.ll_join_chars, v_length,
                                         v_items, v_tp)
            else:
                raise TyperError("''.join() of non-string list: %r" % r_lst)
            return hop.gendirectcall(llfn, v_length, v_items)
        else:
            if r_lst.item_repr == rstr.repr:
                llfn = self.ll.ll_join
            else:
                raise TyperError("sep.join() of non-string list: %r" % r_lst)
            return hop.gendirectcall(llfn, v_str, v_length, v_items)

    def rtype_method_splitlines(self, hop):
        rstr = hop.args_r[0].repr
        if hop.nb_args == 2:
            args = hop.inputargs(rstr.repr, Bool)
        else:
            args = [hop.inputarg(rstr.repr, 0), hop.inputconst(Bool, False)]
        try:
            list_type = hop.r_result.lowleveltype.TO
        except AttributeError:
            list_type = hop.r_result.lowleveltype
        cLIST = hop.inputconst(Void, list_type)
        hop.exception_cannot_occur()
        return hop.gendirectcall(self.ll.ll_splitlines, cLIST, *args)

    def rtype_method_split(self, hop):
        rstr = hop.args_r[0].repr
        if hop.nb_args == 3:
            v_str, v_chr, v_max = hop.inputargs(rstr.repr, rstr.char_repr, Signed)
        else:
            v_str, v_chr = hop.inputargs(rstr.repr, rstr.char_repr)
            v_max = hop.inputconst(Signed, -1)
        try:
            list_type = hop.r_result.lowleveltype.TO
        except AttributeError:
            list_type = hop.r_result.lowleveltype
        cLIST = hop.inputconst(Void, list_type)
        hop.exception_cannot_occur()
        return hop.gendirectcall(self.ll.ll_split_chr, cLIST, v_str, v_chr, v_max)

    def rtype_method_rsplit(self, hop):
        rstr = hop.args_r[0].repr
        if hop.nb_args == 3:
            v_str, v_chr, v_max = hop.inputargs(rstr.repr, rstr.char_repr, Signed)
        else:
            v_str, v_chr = hop.inputargs(rstr.repr, rstr.char_repr)
            v_max = hop.inputconst(Signed, -1)
        try:
            list_type = hop.r_result.lowleveltype.TO
        except AttributeError:
            list_type = hop.r_result.lowleveltype
        cLIST = hop.inputconst(Void, list_type)
        hop.exception_cannot_occur()
        return hop.gendirectcall(self.ll.ll_rsplit_chr, cLIST, v_str, v_chr, v_max)

    def rtype_method_replace(self, hop):
        rstr = hop.args_r[0].repr
        if not (hop.args_r[1] == rstr.char_repr and hop.args_r[2] == rstr.char_repr):
            raise TyperError, 'replace only works for char args'
        v_str, v_c1, v_c2 = hop.inputargs(rstr.repr, rstr.char_repr, rstr.char_repr)
        hop.exception_cannot_occur()
        return hop.gendirectcall(self.ll.ll_replace_chr_chr, v_str, v_c1, v_c2)

    def rtype_int(self, hop):
        hop.has_implicit_exception(ValueError)   # record that we know about it
        string_repr = hop.args_r[0].repr
        if hop.nb_args == 1:
            v_str, = hop.inputargs(string_repr)
            c_base = inputconst(Signed, 10)
            hop.exception_is_here()
            return hop.gendirectcall(self.ll.ll_int, v_str, c_base)
        if not hop.args_r[1] == rint.signed_repr:
            raise TyperError, 'base needs to be an int'
        v_str, v_base= hop.inputargs(string_repr, rint.signed_repr)
        hop.exception_is_here()
        return hop.gendirectcall(self.ll.ll_int, v_str, v_base)

    def rtype_unicode(self, hop):
        if hop.args_s[0].is_constant():
            # convertion errors occur during annotation, so cannot any more:
            hop.exception_cannot_occur()
            return hop.inputconst(hop.r_result, hop.s_result.const)
        repr = hop.args_r[0].repr
        v_str = hop.inputarg(repr, 0)
        if repr == hop.r_result: # the argument is a unicode string already
            hop.exception_cannot_occur()
            return v_str
        hop.exception_is_here()
        return hop.gendirectcall(self.ll.ll_str2unicode, v_str)

    def rtype_bytearray(self, hop):
        if hop.args_s[0].is_constant():
            # convertion errors occur during annotation, so cannot any more:
            hop.exception_cannot_occur()
            return hop.inputconst(hop.r_result, hop.s_result.const)
        hop.exception_is_here()
        return hop.gendirectcall(self.ll.ll_str2bytearray,
                                 hop.inputarg(hop.args_r[0].repr, 0))

    def rtype_method_decode(self, hop):
        if not hop.args_s[1].is_constant():
            raise TyperError("encoding must be a constant")
        encoding = hop.args_s[1].const
        v_self = hop.inputarg(self.repr, 0)
        hop.exception_is_here()
        if encoding == 'ascii':
            return hop.gendirectcall(self.ll.ll_str2unicode, v_self)
        elif encoding == 'latin-1':
            return hop.gendirectcall(self.ll_decode_latin1, v_self)
        elif encoding == 'utf-8':
            self.ensure_ll_decode_utf8()
            return hop.gendirectcall(self.ll_decode_utf8, v_self)
        else:
            raise TyperError("encoding %s not implemented" % (encoding, ))

    def rtype_float(self, hop):
        hop.has_implicit_exception(ValueError)   # record that we know about it
        string_repr = hop.args_r[0].repr
        v_str, = hop.inputargs(string_repr)
        hop.exception_is_here()
        return hop.gendirectcall(self.ll.ll_float, v_str)

    def ll_str(self, s):
        if s:
            return s
        else:
            return self.ll.ll_constant('None')

class __extend__(AbstractUnicodeRepr):
    def rtype_method_encode(self, hop):
        if not hop.args_s[1].is_constant():
            raise TyperError("encoding must be constant")
        encoding = hop.args_s[1].const
        if encoding == "ascii" and self.lowleveltype == UniChar:
            expect = UniChar             # only for unichar.encode('ascii')
        else:
            expect = self.repr           # must be a regular unicode string
        v_self = hop.inputarg(expect, 0)
        hop.exception_is_here()
        if encoding == "ascii":
            return hop.gendirectcall(self.ll_str, v_self)
        elif encoding == "latin-1":
            return hop.gendirectcall(self.ll_encode_latin1, v_self)
        elif encoding == 'utf-8':
            self.ensure_ll_encode_utf8()
            return hop.gendirectcall(self.ll_encode_utf8, v_self)
        else:
            raise TyperError("encoding %s not implemented" % (encoding, ))


class __extend__(pairtype(AbstractStringRepr, Repr)):
    def rtype_mod((r_str, _), hop):
        # for the case where the 2nd argument is a tuple, see the
        # overriding rtype_mod() below
        return r_str.ll.do_stringformat(hop, [(hop.args_v[1], hop.args_r[1])])


class __extend__(pairtype(AbstractStringRepr, IntegerRepr)):
    def rtype_getitem((r_str, r_int), hop, checkidx=False):
        string_repr = r_str.repr
        v_str, v_index = hop.inputargs(string_repr, Signed)
        if checkidx:
            if hop.args_s[1].nonneg:
                llfn = r_str.ll.ll_stritem_nonneg_checked
            else:
                llfn = r_str.ll.ll_stritem_checked
        else:
            if hop.args_s[1].nonneg:
                llfn = r_str.ll.ll_stritem_nonneg
            else:
                llfn = r_str.ll.ll_stritem
        if checkidx:
            hop.exception_is_here()
        else:
            hop.exception_cannot_occur()
        return hop.gendirectcall(llfn, v_str, v_index)

    rtype_getitem_key = rtype_getitem

    def rtype_getitem_idx((r_str, r_int), hop):
        return pair(r_str, r_int).rtype_getitem(hop, checkidx=True)

    rtype_getitem_idx_key = rtype_getitem_idx

    def rtype_mul((r_str, r_int), hop):
        str_repr = r_str.repr
        v_str, v_int = hop.inputargs(str_repr, Signed)
        return hop.gendirectcall(r_str.ll.ll_str_mul, v_str, v_int)
    rtype_inplace_mul = rtype_mul

class __extend__(pairtype(IntegerRepr, AbstractStringRepr)):
    def rtype_mul((r_int, r_str), hop):
        return pair(r_str, r_int).rtype_mul(hop)
    rtype_inplace_mul = rtype_mul


class __extend__(AbstractStringRepr):

    def rtype_getslice(r_str, hop):
        string_repr = r_str.repr
        v_str = hop.inputarg(string_repr, arg=0)
        kind, vlist = hop.decompose_slice_args()
        ll_fn = getattr(r_str.ll, 'll_stringslice_%s' % (kind,))
        return hop.gendirectcall(ll_fn, v_str, *vlist)

class __extend__(pairtype(AbstractStringRepr, AbstractStringRepr)):
    def rtype_add((r_str1, r_str2), hop):
        str1_repr = r_str1.repr
        str2_repr = r_str2.repr
        if hop.s_result.is_constant():
            return hop.inputconst(str1_repr, hop.s_result.const)
        v_str1, v_str2 = hop.inputargs(str1_repr, str2_repr)
        return hop.gendirectcall(r_str1.ll.ll_strconcat, v_str1, v_str2)
    rtype_inplace_add = rtype_add

    def rtype_eq((r_str1, r_str2), hop):
        v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr)
        return hop.gendirectcall(r_str1.ll.ll_streq, v_str1, v_str2)

    def rtype_ne((r_str1, r_str2), hop):
        v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr)
        vres = hop.gendirectcall(r_str1.ll.ll_streq, v_str1, v_str2)
        return hop.genop('bool_not', [vres], resulttype=Bool)

    def rtype_lt((r_str1, r_str2), hop):
        v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr)
        vres = hop.gendirectcall(r_str1.ll.ll_strcmp, v_str1, v_str2)
        return hop.genop('int_lt', [vres, hop.inputconst(Signed, 0)],
                         resulttype=Bool)

    def rtype_le((r_str1, r_str2), hop):
        v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr)
        vres = hop.gendirectcall(r_str1.ll.ll_strcmp, v_str1, v_str2)
        return hop.genop('int_le', [vres, hop.inputconst(Signed, 0)],
                         resulttype=Bool)

    def rtype_ge((r_str1, r_str2), hop):
        v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr)
        vres = hop.gendirectcall(r_str1.ll.ll_strcmp, v_str1, v_str2)
        return hop.genop('int_ge', [vres, hop.inputconst(Signed, 0)],
                         resulttype=Bool)

    def rtype_gt((r_str1, r_str2), hop):
        v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr)
        vres = hop.gendirectcall(r_str1.ll.ll_strcmp, v_str1, v_str2)
        return hop.genop('int_gt', [vres, hop.inputconst(Signed, 0)],
                         resulttype=Bool)

    def rtype_contains((r_str1, r_str2), hop):
        v_str1, v_str2 = hop.inputargs(r_str1.repr, r_str2.repr)
        v_end = hop.gendirectcall(r_str1.ll.ll_strlen, v_str1)
        vres = hop.gendirectcall(r_str1.ll.ll_find, v_str1, v_str2,
                                 hop.inputconst(Signed, 0), v_end)
        hop.exception_cannot_occur()
        return hop.genop('int_ne', [vres, hop.inputconst(Signed, -1)],
                         resulttype=Bool)


class __extend__(pairtype(AbstractStringRepr, AbstractCharRepr),
                 pairtype(AbstractUnicodeRepr, AbstractUniCharRepr)):
    def rtype_contains((r_str, r_chr), hop):
        string_repr = r_str.repr
        char_repr = r_chr.char_repr
        v_str, v_chr = hop.inputargs(string_repr, char_repr)
        hop.exception_cannot_occur()
        return hop.gendirectcall(r_str.ll.ll_contains, v_str, v_chr)

class __extend__(pairtype(AbstractStringRepr, AbstractTupleRepr)):
    def rtype_mod((r_str, r_tuple), hop):
        r_tuple = hop.args_r[1]
        v_tuple = hop.args_v[1]

        sourcevars = []
        for i, r_arg in enumerate(r_tuple.external_items_r):
            v_item = r_tuple.getitem(hop.llops, v_tuple, i)
            sourcevars.append((v_item, r_arg))

        return r_str.ll.do_stringformat(hop, sourcevars)


class __extend__(AbstractCharRepr):
    def ll_str(self, ch):
        return self.ll.ll_chr2str(ch)

class __extend__(AbstractUniCharRepr):
    def ll_str(self, ch):
        # xxx suboptimal, maybe
        return str(unicode(ch))

    def ll_unicode(self, ch):
        return unicode(ch)

class __extend__(AbstractCharRepr,
                 AbstractUniCharRepr):

    def convert_const(self, value):
        if not isinstance(value, str) or len(value) != 1:
            raise TyperError("not a character: %r" % (value,))
        return value

    def get_ll_eq_function(self):
        return None

    def get_ll_hash_function(self):
        return self.ll.ll_char_hash

    get_ll_fasthash_function = get_ll_hash_function

    def rtype_len(_, hop):
        return hop.inputconst(Signed, 1)

    def rtype_is_true(_, hop):
        assert not hop.args_s[0].can_be_None
        return hop.inputconst(Bool, True)

    def rtype_ord(_, hop):
        repr = hop.args_r[0].char_repr
        vlist = hop.inputargs(repr)
        return hop.genop('cast_char_to_int', vlist, resulttype=Signed)

    def _rtype_method_isxxx(_, llfn, hop):
        repr = hop.args_r[0].char_repr
        vlist = hop.inputargs(repr)
        hop.exception_cannot_occur()
        return hop.gendirectcall(llfn, vlist[0])

    def rtype_method_isspace(self, hop):
        return self._rtype_method_isxxx(self.ll.ll_char_isspace, hop)
    def rtype_method_isdigit(self, hop):
        return self._rtype_method_isxxx(self.ll.ll_char_isdigit, hop)
    def rtype_method_isalpha(self, hop):
        return self._rtype_method_isxxx(self.ll.ll_char_isalpha, hop)
    def rtype_method_isalnum(self, hop):
        return self._rtype_method_isxxx(self.ll.ll_char_isalnum, hop)
    def rtype_method_isupper(self, hop):
        return self._rtype_method_isxxx(self.ll.ll_char_isupper, hop)
    def rtype_method_islower(self, hop):
        return self._rtype_method_isxxx(self.ll.ll_char_islower, hop)

class __extend__(pairtype(AbstractCharRepr, IntegerRepr),
                 pairtype(AbstractUniCharRepr, IntegerRepr)):

    def rtype_mul((r_chr, r_int), hop):
        char_repr = r_chr.char_repr
        v_char, v_int = hop.inputargs(char_repr, Signed)
        return hop.gendirectcall(r_chr.ll.ll_char_mul, v_char, v_int)
    rtype_inplace_mul = rtype_mul

class __extend__(pairtype(IntegerRepr, AbstractCharRepr),
                 pairtype(IntegerRepr, AbstractUniCharRepr)):
    def rtype_mul((r_int, r_chr), hop):
        char_repr = r_chr.char_repr
        v_int, v_char = hop.inputargs(Signed, char_repr)
        return hop.gendirectcall(r_chr.ll.ll_char_mul, v_char, v_int)
    rtype_inplace_mul = rtype_mul

class __extend__(pairtype(AbstractCharRepr, AbstractCharRepr)):
    def rtype_eq(_, hop): return _rtype_compare_template(hop, 'eq')
    def rtype_ne(_, hop): return _rtype_compare_template(hop, 'ne')
    def rtype_lt(_, hop): return _rtype_compare_template(hop, 'lt')
    def rtype_le(_, hop): return _rtype_compare_template(hop, 'le')
    def rtype_gt(_, hop): return _rtype_compare_template(hop, 'gt')
    def rtype_ge(_, hop): return _rtype_compare_template(hop, 'ge')

#Helper functions for comparisons

def _rtype_compare_template(hop, func):
    rstr = hop.rtyper.type_system.rstr
    vlist = hop.inputargs(rstr.char_repr, rstr.char_repr)
    return hop.genop('char_'+func, vlist, resulttype=Bool)

class __extend__(AbstractUniCharRepr):

    def convert_const(self, value):
        if isinstance(value, str):
            value = unicode(value)
        if not isinstance(value, unicode) or len(value) != 1:
            raise TyperError("not a unicode character: %r" % (value,))
        return value

    def get_ll_eq_function(self):
        return None

    def get_ll_hash_function(self):
        return self.ll.ll_unichar_hash

    get_ll_fasthash_function = get_ll_hash_function

##    def rtype_len(_, hop):
##        return hop.inputconst(Signed, 1)
##
##    def rtype_is_true(_, hop):
##        assert not hop.args_s[0].can_be_None
##        return hop.inputconst(Bool, True)

    def rtype_ord(_, hop):
        rstr = hop.rtyper.type_system.rstr
        vlist = hop.inputargs(rstr.unichar_repr)
        return hop.genop('cast_unichar_to_int', vlist, resulttype=Signed)


class __extend__(pairtype(AbstractUniCharRepr, AbstractUniCharRepr),
                 pairtype(AbstractCharRepr, AbstractUniCharRepr),
                 pairtype(AbstractUniCharRepr, AbstractCharRepr)):
    def rtype_eq(_, hop): return _rtype_unchr_compare_template(hop, 'eq')
    def rtype_ne(_, hop): return _rtype_unchr_compare_template(hop, 'ne')
##    def rtype_lt(_, hop): return _rtype_unchr_compare_template(hop, 'lt')
##    def rtype_le(_, hop): return _rtype_unchr_compare_template(hop, 'le')
##    def rtype_gt(_, hop): return _rtype_unchr_compare_template(hop, 'gt')
##    def rtype_ge(_, hop): return _rtype_unchr_compare_template(hop, 'ge')

#Helper functions for comparisons

def _rtype_unchr_compare_template(hop, func):
    rstr = hop.rtyper.type_system.rstr
    vlist = hop.inputargs(rstr.unichar_repr, rstr.unichar_repr)
    return hop.genop('unichar_'+func, vlist, resulttype=Bool)


#
# _________________________ Conversions _________________________

class __extend__(pairtype(AbstractCharRepr, AbstractStringRepr),
                 pairtype(AbstractUniCharRepr, AbstractUnicodeRepr)):
    def convert_from_to((r_from, r_to), v, llops):
        rstr = llops.rtyper.type_system.rstr
        if (r_from == rstr.char_repr and r_to == rstr.string_repr) or\
           (r_from == rstr.unichar_repr and r_to == rstr.unicode_repr):
            return llops.gendirectcall(r_from.ll.ll_chr2str, v)
        return NotImplemented

class __extend__(pairtype(AbstractStringRepr, AbstractCharRepr)):
    def convert_from_to((r_from, r_to), v, llops):
        rstr = llops.rtyper.type_system.rstr
        if r_from == rstr.string_repr and r_to == rstr.char_repr:
            c_zero = inputconst(Signed, 0)
            return llops.gendirectcall(r_from.ll.ll_stritem_nonneg, v, c_zero)
        return NotImplemented

class __extend__(pairtype(AbstractCharRepr, AbstractUniCharRepr)):
    def convert_from_to((r_from, r_to), v, llops):
        v2 = llops.genop('cast_char_to_int', [v], resulttype=Signed)
        return llops.genop('cast_int_to_unichar', [v2], resulttype=UniChar)

# ____________________________________________________________
#
#  Iteration.

class AbstractStringIteratorRepr(IteratorRepr):

    def newiter(self, hop):
        string_repr = hop.args_r[0].repr
        v_str, = hop.inputargs(string_repr)
        return hop.gendirectcall(self.ll_striter, v_str)

    def rtype_next(self, hop):
        v_iter, = hop.inputargs(self)
        hop.has_implicit_exception(StopIteration) # record that we know about it
        hop.exception_is_here()
        return hop.gendirectcall(self.ll_strnext, v_iter)


# ____________________________________________________________
#
#  Low-level methods.  These can be run for testing, but are meant to
#  be direct_call'ed from rtyped flow graphs, which means that they will
#  get flowed and annotated, mostly with SomePtr.
#

# this class contains low level helpers used both by lltypesystem and
# ootypesystem; each typesystem should subclass it and add its own
# primitives.
class AbstractLLHelpers:
    __metaclass__ = StaticMethods

    def ll_isdigit(s):
        from pypy.rpython.annlowlevel import hlstr

        s = hlstr(s)
        if not s:
            return False
        for ch in s:
            if not ch.isdigit():
                return False
        return True

    def ll_isalpha(s):
        from pypy.rpython.annlowlevel import hlstr

        s = hlstr(s)
        if not s:
            return False
        for ch in s:
            if not ch.isalpha():
                return False
        return True

    def ll_char_isspace(ch):
        c = ord(ch)
        return c == 32 or (9 <= c <= 13)   # c in (9, 10, 11, 12, 13, 32)

    def ll_char_isdigit(ch):
        c = ord(ch)
        return c <= 57 and c >= 48

    def ll_char_isalpha(ch):
        c = ord(ch)
        if c >= 97:
            return c <= 122
        else:
            return 65 <= c <= 90

    def ll_char_isalnum(ch):
        c = ord(ch)
        if c >= 65:
            if c >= 97:
                return c <= 122
            else:
                return c <= 90
        else:
            return 48 <= c <= 57

    def ll_char_isupper(ch):
        c = ord(ch)
        return 65 <= c <= 90

    def ll_char_islower(ch):
        c = ord(ch)
        return 97 <= c <= 122

    def ll_upper_char(ch):
        if 'a' <= ch <= 'z':
            ch = chr(ord(ch) - 32)
        return ch

    def ll_lower_char(ch):
        if 'A' <= ch <= 'Z':
            ch = chr(ord(ch) + 32)
        return ch

    def ll_char_hash(ch):
        return ord(ch)

    def ll_unichar_hash(ch):
        return ord(ch)

    def ll_str_is_true(cls, s):
        # check if a string is True, allowing for None
        return bool(s) and cls.ll_strlen(s) != 0
    ll_str_is_true = classmethod(ll_str_is_true)

    def ll_stritem_nonneg_checked(cls, s, i):
        if i >= cls.ll_strlen(s):
            raise IndexError
        return cls.ll_stritem_nonneg(s, i)
    ll_stritem_nonneg_checked = classmethod(ll_stritem_nonneg_checked)

    def ll_stritem(cls, s, i):
        if i < 0:
            i += cls.ll_strlen(s)
        return cls.ll_stritem_nonneg(s, i)
    ll_stritem = classmethod(ll_stritem)

    def ll_stritem_checked(cls, s, i):
        length = cls.ll_strlen(s)
        if i < 0:
            i += length
        if i >= length or i < 0:
            raise IndexError
        return cls.ll_stritem_nonneg(s, i)
    ll_stritem_checked = classmethod(ll_stritem_checked)

    def parse_fmt_string(fmt):
        # we support x, d, s, f, [r]
        it = iter(fmt)
        r = []
        curstr = ''
        for c in it:
            if c == '%':
                f = it.next()
                if f == '%':
                    curstr += '%'
                    continue

                if curstr:
                    r.append(curstr)
                curstr = ''
                if f not in 'xdosrf':
                    raise TyperError("Unsupported formatting specifier: %r in %r" % (f, fmt))

                r.append((f,))
            else:
                curstr += c
        if curstr:
            r.append(curstr)
        return r

    def ll_float(ll_str):
        from pypy.rpython.annlowlevel import hlstr
        from pypy.rlib.rfloat import rstring_to_float
        s = hlstr(ll_str)
        assert s is not None

        n = len(s)
        beg = 0
        while beg < n:
            if s[beg] == ' ':
                beg += 1
            else:
                break
        if beg == n:
            raise ValueError
        end = n-1
        while end >= 0:
            if s[end] == ' ':
                end -= 1
            else:
                break
        assert end >= 0
        return rstring_to_float(s[beg:end+1])

    def ll_splitlines(cls, LIST, ll_str, keep_newlines):
        from pypy.rpython.annlowlevel import hlstr
        s = hlstr(ll_str)
        STR = typeOf(ll_str)
        strlen = len(s)
        i = 0
        j = 0
        # The annotator makes sure this list is resizable.
        res = LIST.ll_newlist(0)
        while j < strlen:
            while i < strlen and s[i] != '\n' and s[i] != '\r':
                i += 1
            eol = i
            if i < strlen:
                if s[i] == '\r' and i + 1 < strlen and s[i + 1] == '\n':
                    i += 2
                else:
                    i += 1
                if keep_newlines:
                    eol = i
            list_length = res.ll_length()
            res._ll_resize_ge(list_length + 1)
            item = cls.ll_stringslice_startstop(ll_str, j, eol)
            res.ll_setitem_fast(list_length, item)
            j = i
        if j < strlen:
            list_length = res.ll_length()
            res._ll_resize_ge(list_length + 1)
            item = cls.ll_stringslice_startstop(ll_str, j, strlen)
            res.ll_setitem_fast(list_length, item)
        return res
    ll_splitlines = classmethod(ll_splitlines)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.