Revers strings in micropython (NotImplementedError with slices as [::-1])

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
ropod7
Posts: 6
Joined: Mon Feb 29, 2016 7:01 pm

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Post by ropod7 » Wed Mar 02, 2016 12:54 pm

pythoncoder,
I will try it soon! Thank you.

I have an other solution:
[youtube]thBrAjPJL4c[/youtube]
https://youtu.be/thBrAjPJL4c

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Post by SpotlightKid » Wed Mar 02, 2016 5:19 pm

@ropod7: pythoncoder's byteswap function is buggy. Roberthh's reverse function works.

ropod7
Posts: 6
Joined: Mon Feb 29, 2016 7:01 pm

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Post by ropod7 » Wed Mar 02, 2016 6:47 pm

[youtube]m7FUieXfCRw[/youtube]

Roberthh's reverse function works!

Little update.

https://www.youtube.com/watch?v=m7FUieXfCRw

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Post by pythoncoder » Thu Mar 03, 2016 1:32 pm

Roberthh 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)
Oh dear, you've got me bang to rights ;) This fixes it

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.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Post by Roberthh » Thu Mar 03, 2016 2:23 pm

Almost, sorry. Please check the loop iterations vs. len value, e.g. for len = 0 or 2

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Change endian-ness of a bytearray of half words

Post by pythoncoder » Fri Mar 04, 2016 6:27 am

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.

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.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Change endian-ness of a bytearray of half words

Post by Roberthh » Fri Mar 04, 2016 7:46 am

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

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Post by SpotlightKid » Tue Mar 08, 2016 12:55 am

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

ichtar
Posts: 3
Joined: Mon Mar 01, 2021 10:14 am

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Post by ichtar » Thu Mar 25, 2021 6:52 am

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

leosok
Posts: 18
Joined: Sun May 02, 2021 10:10 pm

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Post by leosok » Fri Jan 07, 2022 10:53 pm

+1 to @ichtar. Your solution works. :idea:

Post Reply