Page 1 of 3

Removing Nulls from strings

Posted: Mon Apr 11, 2022 7:58 am
by Simon Knight
I have just been bitten by the null character which has caused me to lose the extension from four thousand odd image files. Thankfully I have a back up but owing to complications the restore process is going to take approximately twenty four hours. So this is important to me.

I thought I had trapped the null character but it appears to me that the command Replacetext(tString,null,"") does not work.

I believe that this is a bug or at least a serious limitation of the command but seek confirmation just in case I have made a silly error.

I have attached a stack that contains part of a file name that has Ascii 0 after a "W" character in a field. The stack uses two commands that should remove the null but only the second does.

What do you think?

S
Null-Hunt.livecode.zip
Zip of a Livecode stack
(1.58 KiB) Downloaded 161 times

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 9:55 am
by stam
Simon Knight wrote:
Mon Apr 11, 2022 7:58 am
I thought I had trapped the null character but it appears to me that the command Replacetext(tString,null,"") does not work.
<snip>
I believe that this is a bug or at least a serious limitation of the command but seek confirmation just in case I have made a silly error.
<snip>
As I recently realised in my bug triage post here, replaceText uses regex!
This is actually expected behaviour and not a bug ;)
DICTIONARY entry for replaceText wrote:Summary: Searches for a regular expression and replaces the portions that match the regular expression.
As \0 is the regex form for the null character, the correct form is:

Code: Select all

put replacetext(tProblem, "\0","") into tProblemCopy
A quick test with your stack shows that this works but do confirm...
HTH
Stam

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 10:51 am
by Simon Knight
as \0 is the regex form for the null character, the correct form is:
Good to know but it would be ever better if the compiler recognised "null" as such when used with the command.

S

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 11:10 am
by stam
Simon Knight wrote:
Mon Apr 11, 2022 10:51 am
Good to know but it would be ever better if the compiler recognised "null" as such when used with the command.
That's a slightly unfair comment ;)
The documentation specifies this is for regex, not free text search. The fact that it very often works with normal strings is only because unwittingly this often coincides with a string that can be used with regular expressions.

Even the autocomplete suggestion tells you this:
replaceText(stringToChange, matchExpression, replacementString)
It might be a bit much to ask the engine to do regex but then also replace keywords for regular expressions on your behalf. Null is simple, but where does that end? And how would the engine differentiate null from meaning the string \0 or the function numToNativeChar(0)?

As you already point out in your stack there are non-regex ways of doing this that do work, and keywords like null are recognised automatically, so you don't have to rely on replaceText()...

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 11:12 am
by richmond62
Screen Shot 2022-04-11 at 1.11.13 PM.png
-

Code: Select all

on mouseUp
   put empty into fld "FIXED"
   put fld "ProblemName" into PN
   repeat until PN is empty
      put char 1 of PN into FX
      delete char 1 of PN
      put codePointToNum(FX) into MNUM
      if MNUM is (0x0) then
         --- do nix ---
      else
         put FX after fld "FIXED"
         end if
   end repeat
end mouseUp

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 11:15 am
by stam
No need for all that Richmond, Simon already has a working version of this that is simply one line:

Code: Select all

replace null with "" in tProblem
his issue was that replaceText(tProblem, null, "") doesn't work. That's because the find criterion is regex and not free text or keywords, so the syntactically correct way of doing this is

Code: Select all

replaceText(tProblem, "\0", "")

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 11:23 am
by richmond62
No need for all that Richmond
Possibly: BUT my method replaces the NULL character with NOTHING while that
other method replaces it with a SPACE.
-
Screen Shot 2022-04-11 at 1.20.32 PM.png
-

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 11:27 am
by stam
richmond62 wrote:
Mon Apr 11, 2022 11:23 am
Possibly: BUT my method replaces the NULL character with NOTHING while that
other method replaces it with a SPACE.
the replacement string is "", not " ". Nothing is being replaced with a space!
You can just change this to empty if that makes it clearer :)

Code: Select all

replace null with empty in tProblem
or

Code: Select all

replaceText(tProblem, "\0", empty)

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 11:32 am
by richmond62
the replacement string is "", not " ".
Aha.

Don't you get a nice, warm, comfy feeling that that problem can be solved very easily in at least 2 ways? 8)

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 12:26 pm
by Simon Knight
# Stam - its a fair cop, but I'm still mad with myself for creating a way of trapping the error that did not work - grrrrrrr

#Richmond62 Love the button by the way!

S

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 3:23 pm
by dunbarx
Stam.

I read your posts here with interest. I see that "replaceText" lives strictly within regex. But, since "null' is a constant in LC, then could not the OP simply use the "replace" command?

Code: Select all

replace null with "" in yourText
Craig

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 3:26 pm
by Klaus
Yep, that's what Stam proposed and wrote a couple of hours ago. :-)

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 3:28 pm
by dunbarx
Ah.

OK. Missed that.

Craig

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 3:39 pm
by stam
Yep both ways are valid and largely up to personal taste but if you're a bit handy with regex then replaceText() comes into it's own.

On the matter of regex, http://regex101.com is my currently favourite tool, not in the least as it has a live explanation of each symbol as you type and a handy searchable quick reference - for example just type null and you'll get the info you'll need and so on. But regex still bends my mind a bit ;)

Re: Removing Nulls from strings

Posted: Mon Apr 11, 2022 7:41 pm
by richmond62
both ways are valid
All 3 ways are valid!