collapse some std imports

This commit is contained in:
Meghan Denny 2021-09-30 18:47:19 -07:00
parent 86e370add0
commit 164e078dbb
3 changed files with 26 additions and 29 deletions

View File

@ -1,5 +1,4 @@
const std = @import("std"); const std = @import("std");
const mem = std.mem;
const RequestHeader = struct { const RequestHeader = struct {
content_length: usize, content_length: usize,
@ -7,12 +6,12 @@ const RequestHeader = struct {
/// null implies "application/vscode-jsonrpc; charset=utf-8" /// null implies "application/vscode-jsonrpc; charset=utf-8"
content_type: ?[]const u8, content_type: ?[]const u8,
pub fn deinit(self: @This(), allocator: *mem.Allocator) void { pub fn deinit(self: @This(), allocator: *std.mem.Allocator) void {
if (self.content_type) |ct| allocator.free(ct); if (self.content_type) |ct| allocator.free(ct);
} }
}; };
pub fn readRequestHeader(allocator: *mem.Allocator, instream: anytype) !RequestHeader { pub fn readRequestHeader(allocator: *std.mem.Allocator, instream: anytype) !RequestHeader {
var r = RequestHeader{ var r = RequestHeader{
.content_length = undefined, .content_length = undefined,
.content_type = null, .content_type = null,
@ -26,14 +25,14 @@ pub fn readRequestHeader(allocator: *mem.Allocator, instream: anytype) !RequestH
if (header.len == 0 or header[header.len - 1] != '\r') return error.MissingCarriageReturn; if (header.len == 0 or header[header.len - 1] != '\r') return error.MissingCarriageReturn;
if (header.len == 1) break; if (header.len == 1) break;
const header_name = header[0 .. mem.indexOf(u8, header, ": ") orelse return error.MissingColon]; const header_name = header[0 .. std.mem.indexOf(u8, header, ": ") orelse return error.MissingColon];
const header_value = header[header_name.len + 2 .. header.len - 1]; const header_value = header[header_name.len + 2 .. header.len - 1];
if (mem.eql(u8, header_name, "Content-Length")) { if (std.mem.eql(u8, header_name, "Content-Length")) {
if (header_value.len == 0) return error.MissingHeaderValue; if (header_value.len == 0) return error.MissingHeaderValue;
r.content_length = std.fmt.parseInt(usize, header_value, 10) catch return error.InvalidContentLength; r.content_length = std.fmt.parseInt(usize, header_value, 10) catch return error.InvalidContentLength;
has_content_length = true; has_content_length = true;
} else if (mem.eql(u8, header_name, "Content-Type")) { } else if (std.mem.eql(u8, header_name, "Content-Type")) {
r.content_type = try mem.dupe(allocator, u8, header_value); r.content_type = try std.mem.dupe(allocator, u8, header_value);
} else { } else {
return error.UnknownHeader; return error.UnknownHeader;
} }

View File

@ -1,6 +1,5 @@
const std = @import("std"); const std = @import("std");
// LSP types // LSP types
const json = std.json;
pub const Position = struct { pub const Position = struct {
line: i64, line: i64,
@ -90,8 +89,8 @@ pub const DiagnosticSeverity = enum(i64) {
Information = 3, Information = 3,
Hint = 4, Hint = 4,
pub fn jsonStringify(value: DiagnosticSeverity, options: json.StringifyOptions, out_stream: anytype) !void { pub fn jsonStringify(value: DiagnosticSeverity, options: std.json.StringifyOptions, out_stream: anytype) !void {
try json.stringify(@enumToInt(value), options, out_stream); try std.json.stringify(@enumToInt(value), options, out_stream);
} }
}; };
@ -193,8 +192,8 @@ pub const InsertTextFormat = enum(i64) {
PlainText = 1, PlainText = 1,
Snippet = 2, Snippet = 2,
pub fn jsonStringify(value: InsertTextFormat, options: json.StringifyOptions, out_stream: anytype) !void { pub fn jsonStringify(value: InsertTextFormat, options: std.json.StringifyOptions, out_stream: anytype) !void {
try json.stringify(@enumToInt(value), options, out_stream); try std.json.stringify(@enumToInt(value), options, out_stream);
} }
}; };
@ -226,8 +225,8 @@ pub const CompletionItem = struct {
Operator = 24, Operator = 24,
TypeParameter = 25, TypeParameter = 25,
pub fn jsonStringify(value: Kind, options: json.StringifyOptions, out_stream: anytype) !void { pub fn jsonStringify(value: Kind, options: std.json.StringifyOptions, out_stream: anytype) !void {
try json.stringify(@enumToInt(value), options, out_stream); try std.json.stringify(@enumToInt(value), options, out_stream);
} }
}; };
@ -270,8 +269,8 @@ pub const DocumentSymbol = struct {
Operator = 25, Operator = 25,
TypeParameter = 26, TypeParameter = 26,
pub fn jsonStringify(value: Kind, options: json.StringifyOptions, out_stream: anytype) !void { pub fn jsonStringify(value: Kind, options: std.json.StringifyOptions, out_stream: anytype) !void {
try json.stringify(@enumToInt(value), options, out_stream); try std.json.stringify(@enumToInt(value), options, out_stream);
} }
}; };

View File

@ -1,5 +1,4 @@
const std = @import("std"); const std = @import("std");
const mem = std.mem;
// http://tools.ietf.org/html/rfc3986#section-2.2 // http://tools.ietf.org/html/rfc3986#section-2.2
const reserved_chars = &[_]u8{ const reserved_chars = &[_]u8{
@ -19,7 +18,7 @@ const reserved_escapes = blk: {
}; };
/// Returns a URI from a path, caller owns the memory allocated with `allocator` /// Returns a URI from a path, caller owns the memory allocated with `allocator`
pub fn fromPath(allocator: *mem.Allocator, path: []const u8) ![]const u8 { pub fn fromPath(allocator: *std.mem.Allocator, path: []const u8) ![]const u8 {
if (path.len == 0) return ""; if (path.len == 0) return "";
const prefix = if (std.builtin.os.tag == .windows) "file:///" else "file://"; const prefix = if (std.builtin.os.tag == .windows) "file:///" else "file://";
@ -29,7 +28,7 @@ pub fn fromPath(allocator: *mem.Allocator, path: []const u8) ![]const u8 {
for (path) |char| { for (path) |char| {
if (char == std.fs.path.sep) { if (char == std.fs.path.sep) {
try buf.append('/'); try buf.append('/');
} else if (mem.indexOfScalar(u8, reserved_chars, char)) |reserved| { } else if (std.mem.indexOfScalar(u8, reserved_chars, char)) |reserved| {
try buf.appendSlice(&reserved_escapes[reserved]); try buf.appendSlice(&reserved_escapes[reserved]);
} else { } else {
try buf.append(char); try buf.append(char);
@ -40,7 +39,7 @@ pub fn fromPath(allocator: *mem.Allocator, path: []const u8) ![]const u8 {
if (std.builtin.os.tag == .windows) { if (std.builtin.os.tag == .windows) {
if (buf.items.len > prefix.len + 1 and if (buf.items.len > prefix.len + 1 and
std.ascii.isAlpha(buf.items[prefix.len]) and std.ascii.isAlpha(buf.items[prefix.len]) and
mem.startsWith(u8, buf.items[prefix.len + 1 ..], "%3A")) std.mem.startsWith(u8, buf.items[prefix.len + 1 ..], "%3A"))
{ {
buf.items[prefix.len] = std.ascii.toLower(buf.items[prefix.len]); buf.items[prefix.len] = std.ascii.toLower(buf.items[prefix.len]);
} }
@ -51,19 +50,19 @@ pub fn fromPath(allocator: *mem.Allocator, path: []const u8) ![]const u8 {
/// Move along `rel` from `base` with a single allocation. /// Move along `rel` from `base` with a single allocation.
/// `base` is a URI of a folder, `rel` is a raw relative path. /// `base` is a URI of a folder, `rel` is a raw relative path.
pub fn pathRelative(allocator: *mem.Allocator, base: []const u8, rel: []const u8) ![]const u8 { pub fn pathRelative(allocator: *std.mem.Allocator, base: []const u8, rel: []const u8) ![]const u8 {
const max_size = base.len + rel.len * 3 + 1; const max_size = base.len + rel.len * 3 + 1;
var result = try allocator.alloc(u8, max_size); var result = try allocator.alloc(u8, max_size);
errdefer allocator.free(result); errdefer allocator.free(result);
mem.copy(u8, result, base); std.mem.copy(u8, result, base);
var result_index: usize = base.len; var result_index: usize = base.len;
var it = mem.tokenize(u8, rel, "/"); var it = std.mem.tokenize(u8, rel, "/");
while (it.next()) |component| { while (it.next()) |component| {
if (mem.eql(u8, component, ".")) { if (std.mem.eql(u8, component, ".")) {
continue; continue;
} else if (mem.eql(u8, component, "..")) { } else if (std.mem.eql(u8, component, "..")) {
while (true) { while (true) {
if (result_index == 0) if (result_index == 0)
return error.UriBadScheme; return error.UriBadScheme;
@ -75,9 +74,9 @@ pub fn pathRelative(allocator: *mem.Allocator, base: []const u8, rel: []const u8
result[result_index] = '/'; result[result_index] = '/';
result_index += 1; result_index += 1;
for (component) |char| { for (component) |char| {
if (mem.indexOfScalar(u8, reserved_chars, char)) |reserved| { if (std.mem.indexOfScalar(u8, reserved_chars, char)) |reserved| {
const escape = &reserved_escapes[reserved]; const escape = &reserved_escapes[reserved];
mem.copy(u8, result[result_index..], escape); std.mem.copy(u8, result[result_index..], escape);
result_index += escape.len; result_index += escape.len;
} else { } else {
result[result_index] = char; result[result_index] = char;
@ -108,8 +107,8 @@ fn parseHex(c: u8) !u8 {
} }
/// Caller should free memory /// Caller should free memory
pub fn parse(allocator: *mem.Allocator, str: []const u8) ![]u8 { pub fn parse(allocator: *std.mem.Allocator, str: []const u8) ![]u8 {
if (str.len < 7 or !mem.eql(u8, "file://", str[0..7])) return error.UriBadScheme; if (str.len < 7 or !std.mem.eql(u8, "file://", str[0..7])) return error.UriBadScheme;
const uri = try allocator.alloc(u8, str.len - (if (std.fs.path.sep == '\\') 8 else 7)); const uri = try allocator.alloc(u8, str.len - (if (std.fs.path.sep == '\\') 8 else 7));
errdefer allocator.free(uri); errdefer allocator.free(uri);