Skip to content

Skipping the special character in string 5 of SNIP not working #79

@Poikilos

Description

@Poikilos

I renamed a couple variables for clarity.

In this code, i += 1 does nothing, because range is an iterator not a literal < check.

    def findString(self, n: int) -> int:
        '''Find start index of the nth string.
        Is aware of the 2nd version code byte.

        Args:
            n (int): Zero indexed.
        Returns:
            int: 0, or logs and returns -1 if the string isn't found
                within the buffer
        '''

        if n == 0:
            return 1  # first one is automatic
        currentStart = 1
        stringsFound = 0
        # scan over the buffer
        for i in range(1, 252):
            # checking for an end-of-string mark
            if self.data[i] == 0:
                # found one - this ends the stringCount string
                # if that's the request, return start
                if stringsFound == n:
                    return currentStart
                # if not, the _next_ character starts the next string
                currentStart = i+1
                stringsFound += 1
                # special case for the 5th string
                if stringsFound == 4:
                    i += 1
                    currentStart += 1
        # fell out without finding
        return 0

So I assume I should change it like this:

-        for i in range(1, 252):
-        i = 1
+        while i < 252:
  • then at the end of the loop add i += 1 to continue as the old code would:
+            i += 1

?

  • If this is changed, maybe getString could be shorter (It seems to have a workaround, checking for b'\0'). I can't tell until I can understand if findString(4) return is ok or not.

:edit: I forgot to mention there was an actual symptom: There was a case when the manufacturer string started with the EOT character (0x04, shown as ␄ in Terminal) but I haven't been able to make it happen again.

  • It isn't clear what the actual cause was, but that's why I started looking at this method.
  • I suppose it is possible the device's manufacturer string wrongly had an EOT character, but I'm not sure where/if one is supposed to be in its SNIP data.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions