Skip to content

Commit

Permalink
improve t.text
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Nov 26, 2024
1 parent bcbdecd commit 07a7002
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 126 deletions.
63 changes: 63 additions & 0 deletions src/compiler/ast/File.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (C) 2024-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

module ast

import os

@[heap]
pub struct File {
pub:
file string
content string
pub mut:
lines ?[]string
}

pub fn File.new(file string) &File {
content := read_file(file)
return &File{
file: file
content: content
}
}

pub fn File.from_memory(content string) &File {
return &File{
file: '<memory>'
content: content
}
}

pub fn (mut file File) get_lines() []string {
if file.lines != none {
return file.lines
}
lines := file.content.split_into_lines()
file.lines = lines
return lines
}

pub fn read_file(path string) string {
return skip_bom(os.read_file(path) or {
// we use `panic` because this should not happen
panic(err.msg())
})
}

pub fn skip_bom(file_content string) string {
mut raw_text := file_content
// BOM check
if raw_text.len >= 3 {
unsafe {
c_text := raw_text.str
if c_text[0] == 0xEF && c_text[1] == 0xBB && c_text[2] == 0xBF {
// skip three BOM bytes
offset_from_begin := 3
raw_text = tos(c_text[offset_from_begin], vstrlen(c_text) - offset_from_begin)
}
}
}
return raw_text
}
39 changes: 0 additions & 39 deletions src/compiler/ast/SourceFile.v

This file was deleted.

Loading

0 comments on commit 07a7002

Please sign in to comment.