void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) { size_t len = buf_len(full_path); if (len != 0) { size_t last_index = len - 1; if (buf_ptr(full_path)[last_index] == '/') { last_index -= 1; } for (size_t i = last_index;;) { uint8_t c = buf_ptr(full_path)[i]; if (c == '/') { if (out_dirname) { buf_init_from_mem(out_dirname, buf_ptr(full_path), i); } if (out_basename) { buf_init_from_mem(out_basename, buf_ptr(full_path) + i + 1, buf_len(full_path) - (i + 1)); } return; } if (i == 0) break; i -= 1; } } if (out_dirname) buf_init_from_mem(out_dirname, ".", 1); if (out_basename) buf_init_from_buf(out_basename, full_path); }
void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) { int last_index = buf_len(full_path) - 1; if (last_index >= 0 && buf_ptr(full_path)[last_index] == '/') { last_index -= 1; } for (int i = last_index; i >= 0; i -= 1) { uint8_t c = buf_ptr(full_path)[i]; if (c == '/') { buf_init_from_mem(out_dirname, buf_ptr(full_path), i); buf_init_from_mem(out_basename, buf_ptr(full_path) + i + 1, buf_len(full_path) - (i + 1)); return; } } buf_init_from_mem(out_dirname, ".", 1); buf_init_from_buf(out_basename, full_path); }
ErrorMsg *err_msg_create_with_offset(Buf *path, int line, int column, int offset, const char *source, Buf *msg) { ErrorMsg *err_msg = allocate<ErrorMsg>(1); err_msg->path = path; err_msg->line_start = line; err_msg->column_start = column; err_msg->msg = msg; int line_start_offset = offset; for (;;) { if (line_start_offset == 0) { break; } else if (source[line_start_offset] == '\n') { line_start_offset += 1; break; } line_start_offset -= 1; } int line_end_offset = offset; while (source[line_end_offset] && source[line_end_offset] != '\n') { line_end_offset += 1; } buf_init_from_mem(&err_msg->line_buf, source + line_start_offset, line_end_offset - line_start_offset); return err_msg; }
ErrorMsg *err_msg_create_with_line(Buf *path, size_t line, size_t column, Buf *source, ZigList<size_t> *line_offsets, Buf *msg) { ErrorMsg *err_msg = allocate<ErrorMsg>(1); err_msg->path = path; err_msg->line_start = line; err_msg->column_start = column; err_msg->msg = msg; size_t line_start_offset = line_offsets->at(line); size_t end_line = line + 1; size_t line_end_offset = (end_line >= line_offsets->length) ? buf_len(source) : line_offsets->at(line + 1); size_t len = (line_end_offset + 1 > line_start_offset) ? (line_end_offset - line_start_offset - 1) : 0; buf_init_from_mem(&err_msg->line_buf, buf_ptr(source) + line_start_offset, len); return err_msg; }
ErrorMsg *err_msg_create_with_line(Buf *path, int line, int column, Buf *source, ZigList<int> *line_offsets, Buf *msg) { ErrorMsg *err_msg = allocate<ErrorMsg>(1); err_msg->path = path; err_msg->line_start = line; err_msg->column_start = column; err_msg->msg = msg; int line_start_offset = line_offsets->at(line); int end_line = line + 1; int line_end_offset = (end_line >= line_offsets->length) ? buf_len(source) : line_offsets->at(line + 1); int len = line_end_offset - line_start_offset - 1; if (len < 0) { len = 0; } buf_init_from_mem(&err_msg->line_buf, buf_ptr(source) + line_start_offset, len); return err_msg; }