Revers strings in micropython (NotImplementedError with slices as [::-1])
Re: Revers strings in micropython (NotImplementedError with slices as [::-1])
pythoncoder,
I will try it soon! Thank you.
I have an other solution:
[youtube]thBrAjPJL4c[/youtube]
https://youtu.be/thBrAjPJL4c
I will try it soon! Thank you.
I have an other solution:
[youtube]thBrAjPJL4c[/youtube]
https://youtu.be/thBrAjPJL4c
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: Revers strings in micropython (NotImplementedError with slices as [::-1])
@ropod7: pythoncoder's byteswap function is buggy. Roberthh's reverse function works.
Re: Revers strings in micropython (NotImplementedError with slices as [::-1])
[youtube]m7FUieXfCRw[/youtube]
Roberthh's reverse function works!
Little update.
https://www.youtube.com/watch?v=m7FUieXfCRw
Roberthh's reverse function works!
Little update.
https://www.youtube.com/watch?v=m7FUieXfCRw
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Revers strings in micropython (NotImplementedError with slices as [::-1])
Oh dear, you've got me bang to rights This fixes itRoberthh wrote:Hi @pythoncoder, besides the fact, that I should have called my function byteswap, like you did, it seems to me that your code is wrong. You increment the data pointers by two in each loop, but divide the buffer length value by 4, which means that only half of the buffer is processed.
Robert (alias Statler)
Code: Select all
@micropython.asm_thumb
def byteswap(r0, r1): # bytearray, len(bytearray)
mov(r3, 1)
lsr(r1, r3) # divide len by 2
mov(r4, r0)
add(r4, 1) # dest address
label(LOOP)
ldrb(r5, [r0, 0])
ldrb(r6, [r4, 0])
strb(r6, [r0, 0])
strb(r5, [r4, 0])
add(r0, 2)
add(r4, 2)
sub(r1, 1)
bpl(LOOP)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Revers strings in micropython (NotImplementedError with slices as [::-1])
Almost, sorry. Please check the loop iterations vs. len value, e.g. for len = 0 or 2
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Change endian-ness of a bytearray of half words
Yes, it did one iteration too many. Here is a version which validates input, returning 1 on success or 0 on failure (len() of 0 or 1).
I prefer to validate in Python before calling assembler routines. It's easier to read and maintain, simplifies the asm, eases doing something meaningful if validation fails and saves the overhead of calling a function which will do nothing.
I prefer to validate in Python before calling assembler routines. It's easier to read and maintain, simplifies the asm, eases doing something meaningful if validation fails and saves the overhead of calling a function which will do nothing.
Code: Select all
@micropython.asm_thumb
def byteswap(r0, r1): # bytearray, len(bytearray)
cmp(r1, 1)
ble(FAIL)
mov(r3, 1)
lsr(r1, r3) # divide len by 2
mov(r4, r0)
add(r4, 1) # dest address
label(LOOP)
ldrb(r5, [r0, 0])
ldrb(r6, [r4, 0])
strb(r6, [r0, 0])
strb(r5, [r4, 0])
add(r0, 2)
add(r4, 2)
sub(r1, 1)
bne(LOOP)
mov(r0, 1)
b(END)
label(FAIL)
mov(r0, 0)
label(END)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Change endian-ness of a bytearray of half words
Hello @pythoncoder, this is a good example how nasty simple seeming functions can be. My variant, simpler looking, has a fault too. it counts right, and does not execute at a value of 0, BUT fails badly if the counter is odd, by trying to access a byte beyond the buffer limit. Your variant, due to the division, would simply ignore the last byte. Robert
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: Revers strings in micropython (NotImplementedError with slices as [::-1])
So how about this?
Code: Select all
@micropython.asm_thumb
def byteswap(r0, r1):
label(loopstart)
cmp(r1, 1)
ble(loopend)
ldrb(r2, [r0, 0])
ldrb(r3, [r0, 1])
strb(r3, [r0, 0])
strb(r2, [r0, 1])
add(r0, 2)
sub(r1, 2)
b(loopstart)
label(loopend)
def byteswapped(b):
res = bytearray(b)
byteswap(res, len(res))
return res
Re: Revers strings in micropython (NotImplementedError with slices as [::-1])
I know it is a really old post but I was struggling with that too and found that
>>> str="abc"
>>> print(''.join(reversed(str)))
cba
works pretty well for me
>>> str="abc"
>>> print(''.join(reversed(str)))
cba
works pretty well for me
Re: Revers strings in micropython (NotImplementedError with slices as [::-1])
+1 to @ichtar. Your solution works.