|
1 | 1 | use anyhow::{Context, Error}; |
| 2 | +use chardetng::EncodingDetector; |
2 | 3 | use clap::{Parser, CommandFactory}; |
3 | 4 | use clap_complete; |
4 | 5 | use enquote; |
5 | 6 | use std::collections::HashMap; |
6 | | -use std::fs::{read_to_string, File, OpenOptions}; |
| 7 | +use std::fs::{File, OpenOptions}; |
7 | 8 | use std::io::{Read, Write}; |
8 | 9 | use std::path::{Path, PathBuf}; |
9 | 10 | use std::{env, process}; |
@@ -275,7 +276,15 @@ pub fn run_opt_config(printer: &mut Printer, opt: &Opt, config: Config) -> Resul |
275 | 276 | // by textrules to reset their internal state. |
276 | 277 | let _ = linter.textrules_check(TextRuleEvent::StartOfFile, &path, &0); |
277 | 278 |
|
278 | | - let text: String = read_to_string(&path)?; |
| 279 | + let mut file = File::open(&path)?; |
| 280 | + let mut buffer = Vec::new(); |
| 281 | + |
| 282 | + file.read_to_end(&mut buffer)?; |
| 283 | + let mut detector = EncodingDetector::new(); |
| 284 | + detector.feed(&buffer, true); |
| 285 | + let encoding = detector.guess(None, true).decode(&buffer).0; |
| 286 | + |
| 287 | + let text = encoding.into_owned(); |
279 | 288 | let mut beg: usize = 0; |
280 | 289 |
|
281 | 290 | // Iterate over lines in the file, applying each textrule to each |
@@ -1215,4 +1224,40 @@ mod tests { |
1215 | 1224 | let stdout = printer.read_to_string().unwrap(); |
1216 | 1225 | assert_eq!(stdout, expected_contents("dump_filelist_8")); |
1217 | 1226 | } // }}} |
| 1227 | + |
| 1228 | + #[test] |
| 1229 | + fn lint_gbk_encoded_verilog() { |
| 1230 | + use std::fs::File; |
| 1231 | + use std::io::Write; |
| 1232 | + use std::path::Path; |
| 1233 | + |
| 1234 | + // Create a temporary Verilog file |
| 1235 | + let temp_path = Path::new("temp_gbk_verilog.sv"); |
| 1236 | + let mut file = File::create(&temp_path).expect("Failed to create test file"); |
| 1237 | + |
| 1238 | + // Write GBK-encoded Verilog code |
| 1239 | + let gbk_verilog = vec![ |
| 1240 | + 0x0a, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x20, 0x74, 0x6f, 0x70, 0x3b, 0x0a, 0x2f, |
| 1241 | + 0x2f, 0x20, 0xd6, 0xd0, 0xce, 0xc4, 0xd7, 0xa2, 0xca, 0xcd, 0x0a, 0x65, 0x6e, 0x64, |
| 1242 | + 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x0a, |
| 1243 | + ]; |
| 1244 | + file.write_all(&gbk_verilog) |
| 1245 | + .expect("Failed to write test file"); |
| 1246 | + drop(file); // Close the file |
| 1247 | + |
| 1248 | + // Run `svlint` to analyze the file |
| 1249 | + let config: Config = toml::from_str("").unwrap(); |
| 1250 | + let mut args = vec!["svlint"]; |
| 1251 | + args.push(temp_path.to_str().unwrap()); |
| 1252 | + let opt = Opt::parse_from(args.iter()); |
| 1253 | + |
| 1254 | + let mut printer = Printer::new(true); |
| 1255 | + let ret = run_opt_config(&mut printer, &opt, config.clone()); |
| 1256 | + |
| 1257 | + // Clean up the test file |
| 1258 | + std::fs::remove_file(&temp_path).expect("Failed to remove test file"); |
| 1259 | + |
| 1260 | + // Assert that `svlint` successfully processes the GBK-encoded Verilog file |
| 1261 | + assert!(ret.is_ok(), "svlint failed to process GBK-encoded Verilog"); |
| 1262 | + } |
1218 | 1263 | } |
0 commit comments