-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
122 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.