#When loading .txt files, escape characters are handled incorrectly

9 messages · Page 1 of 1 (latest)

rigid plume
#

there is no escaping no... if your file has a \ it will be in the bytes from ReadFile

#

you must have a different bug or issue, if you want to share a complete small program we can help find what is wrong

dense lodge
#

this is the function when i process Strings:

type Token struct {
    TokenType TokenType
    Value     string
    Line      int
    Column    int
}

type Tokenizer struct {
    Data     string
    position int
    column   int
    line     int
    len      int
    tokens   []Token
}

// a bunch of other things

func (tokenizer *Tokenizer) handleStrings() error {
    tokenizer.advance() // Pula primeira aspas
    start := tokenizer.position

    current := tokenizer.getCurrentChar()
    for current != '"' {
        if current == '\n' {
            return fmt.Errorf("Quebra de linha ao declara string. Linha %d Coluna %d", tokenizer.line+1, tokenizer.column+1)
        }

        tokenizer.advance()
        current = tokenizer.getCurrentChar()
    }

    str := tokenizer.Data[start:tokenizer.position]
    tokenizer.addToken(STRING, str)
    return nil
}

In handleStrings, i read the file a byte of a time, And i'm reading the \n in the source like a break line

Example:

.data
    lyric_1: .string " bottles of beer on the wall. \n\0" 
    lyric_2: .string " bottles of beer. \n\0"
    lyric_3: .string "Take one down & pass it around, now there's. \n\0"
    lyric_4: .string "No more bottles of beer on the wall, no more bottles of beer. \n\0"
    lyric_5: .string "Go to the store and buy some more, 99 bottles of beer on the wall... \n\0"
    lyric_6: .string " bottle of beer on the wall. \n\0" 
    lyric_7: .string " bottle of beer. \n\0"

Not a single break link in the strings, but the tokenizer returns a error because of the \n in the first string.

https://github.com/MarceloLuisDantas/Kokonoe-Go the rep if you want.

GitHub

Kokonoe PC em Go. Contribute to MarceloLuisDantas/Kokonoe-Go development by creating an account on GitHub.

#

Sorry, but most of the text are in PT-BR

#

if you want to test, run go run . demos/99_bottles.asm

rigid plume
#

that's not exactly a simple repro 🙂

#

readfile doesn't change the content as you can see ^

#
package main
import (
        "fmt"
        "os"
)
func main() {
        b, _ := os.ReadFile("file.txt")
        fmt.Println(string(b))
}

feel free to try this ^