About Monkey 2 › Forums › Monkey 2 Development › problem with text editing.
This topic contains 6 replies, has 3 voices, and was last updated by
Jesse
2 years, 6 months ago.
-
AuthorPosts
-
September 23, 2016 at 6:21 am #4058
I am trying to load a text file and parse it byte by byte, I can brake it down into lines and display it correctly but when I try to parse it by bytes and add the bytes back up it creates some weird text at the end of each line. I think its a problem with the byte additions in the string class but I need to verify. I have included the text file with a “fnt” extension. And the source code is a small test program:
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839[crayon-5cba8c8b3db71597463150 inline="true" ]#import "<mojo>"#import "<std>"#import "mygliph.fnt"Using mojo..Using std..Function Main()LoadFont("mygliph")End FunctionFunction LoadFont(url:String)Local str:String = "aset::"+url+".fnt"Print str+"**********"Local text:String = LoadString("asset::"+url+".fnt")If text = Null Print "unable to load text"Local lines:= text.Split(String.FromChar(10))lines[4] = lines[4].Replace(" ","")For Local i:Int = 4 Until lines.Length 'go through each lines in the textLocal s:String = ""Print sFor Local j:int = 0 Until lines[i].Length 'go through each character in the lineWhile lines[i][j] <> 32 'select all of the non space characterss += String.FromChar(lines[i][j])j+=1Wend'skip all of the 'space' charactersWhile lines[i][j] = 32 And j < lines[i].Lengthj+=1Wendj -= 1 ' move back to where the last space is.s+="," ' and replace with a comma,NextPrint sNextEnd functionthis is a link to this same source code and text file:
https://www.dropbox.com/s/n0qiv91gxuqg3l1/Archive.zip?dl=0I would appreciate if someone could give me a hand in figuring this out.
September 23, 2016 at 7:21 am #4059not sure what the issue is, as far as I could see the .fnt file looks ok. But the code is a bit mangled.
Here’s a simpler version which reads the file byte by byte and reconstructs each line. gives a very different output:
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364#import "<mojo>"#import "<std>"Using mojo..Using std..#import "mygliph.fnt"Function Main()LoadMyFont("mygliph")End FunctionFunction LoadMyFont(url:String)Local str:String = "aset::"+url+".fnt"Print str+"**********"Local text:String = LoadString("asset::"+url+".fnt")If text = Null thenPrint "unable to load text"ReturnEnd IfLocal k:intLocal line:stringLocal char:stringFor k = 0 To text.Lengthchar = text.Mid( k, 1 )If char < " " ThenPrint lineline = ""Elseline += charEnd IfNext' Local lines:= text.Split(String.FromChar(10))' lines[4] = lines[4].Replace(" ","")' For Local i:Int = 4 Until lines.Length' Local s:String = ""' Print s' For Local j:int = 0 Until lines[i].Length' If lines[i][j] = "@"[0] exit' While lines[i][j] <> 32' s += String.FromChar(lines[i][j])' j+=1' Wend' While lines[i][j] = 32 And j < lines[i].Length' j+=1' Wend' j -= 1' s+=","' Next' Print s' NextEnd functionSeptember 23, 2016 at 12:54 pm #4064did you download and run the code?
your code doesn’t do what I want to do.the problem with my code is that it reads the line and outputs the result but at the end of the line it adds some extra characters, characters that are not in the text file. the picture shows that the last 5 or four characters at each of the lines are extra:

if you compare the output with the txt file you will notice that it does not contain “{char” at the end of each line of the “.fnt” file.if I replace line 27 with this:
s += lines[i].Mid(j,1)
it works fine.September 23, 2016 at 7:39 pm #4075You’re reading past the end of line, you need something like:
Monkey123456789While j < lines[i].Length And lines[i][j]<>32s += String.FromChar(lines[i][j])j+=1WendWhile j < lines[i].Length And lines[i][j] = 32j+=1WendBut mx2’s not helping here – it should be generating something like ‘index out of range’ when you try to do this. Will fix ASAP.
September 23, 2016 at 8:02 pm #4076Thanks.
about reading past the end of the line,it’s not.
if I replace line 27:
s += String.FromChar(lines[i][j])
with this:
s += lines[i].Mid(j,1)
it works fine.September 23, 2016 at 8:45 pm #4077Your first while loops compares char at j with 32, without first checking that j<line.Length. With char index checking on, it indeed fails here on the first line of the text file.
Your second while loop is better, but it checks j<line.length *after* reading char at j so the check is too late.
it works fine.
This is because Mid() uses Slice, which does a range check so it’ll be returning an empty string once you go past end of line. Here, it works for a bit longer with this change but I eventually get a ‘memory access error’.
String char index checking would have made this 100 times easier to find (can’t believe it wasn’t there – sure it used to be), so it’s my fault really…
September 23, 2016 at 9:22 pm #4078As usual I feel stupid. I still don’t get why it goes beyond one and not with the other. I don’t see why the while don’t exit if j >= to line[i].Length. to my understanding it should exit regardless of what follows. Don’t worry i am known to have this kind of moments.
-
AuthorPosts
You must be logged in to reply to this topic.