Embedded nulls cause apparent truncation

  • Chuck

    Chuck - 2011-11-06

    I've got some text lines that have nulls embedded in the middle.     I need the nulls left in place. 
    When I ask for the line by number, I get everything up to the nulls, but no more.
    The len() of the data stops at the null.  The data is still present, it is displayed (including the nulls) and is editable by N++.

    I know GetText returns the entire document but I cannot figure out how to copy the entire line into a buffer where I can use it for other things. 

    I can use the starting character position of the line I want and the start of next line, but my python skills aren't yet strong enough to grab the data between those indices and copy all the bytes out into an array,  Everything I try ends up truncating at the nulls.  

    Brother(stone), can you spare me a line(of code)?

    Thanks in advance!

  • Dave Brotherstone

    Yeah, unfortunately Python strings are also null terminated, which makes it very difficult to read a line with a null in it as a string.  However, with a bit of trickery you can work out that the line contains a null, and then convert it to a python escaped string.

    The idea is that you get the start position of the line, the end position, and then compare that to len(editor.getLine(lineNumber)) where lineNumber is the line you're interested.   If len(…) is shorter, then you know you've got nulls in the line (and you can work out where the first one is).  If you call editor.getCharAt(position) you get an int back, which can be zero.  If you then add \x00 to your python string, you've got a python string that's an accurate representation of the line in Scintilla.  After you've added the null, you can try and get the next part of the string, and see how long that is - if that plus the first part is still shorter than the line, you know you've still got nulls in your line and can repeat the procedure.

    It sounds a bit long winded, but you can probably write a function in a few short lines of python to extract a line with nulls, and use that as a replacement for getLine in your code.

    Good luck,


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks