aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
Diffstat (limited to 'std')
-rw-r--r--std/build.zig18
-rw-r--r--std/fmt.zig33
-rw-r--r--std/math.zig39
3 files changed, 51 insertions, 39 deletions
diff --git a/std/build.zig b/std/build.zig
index f944133d5b..29837b4ba4 100644
--- a/std/build.zig
+++ b/std/build.zig
@@ -488,15 +488,15 @@ pub const Builder = struct {
}
fn typeToEnum(comptime T: type) -> TypeId {
- if (@isInteger(T)) {
- TypeId.Int
- } else if (@isFloat(T)) {
- TypeId.Float
- } else switch (T) {
- bool => TypeId.Bool,
- []const u8 => TypeId.String,
- []const []const u8 => TypeId.List,
- else => @compileError("Unsupported type: " ++ @typeName(T)),
+ switch (@typeId(T)) {
+ builtin.TypeId.Int => TypeId.Int,
+ builtin.TypeId.Float => TypeId.Float,
+ builtin.TypeId.Bool => TypeId.Bool,
+ else => switch (T) {
+ []const u8 => TypeId.String,
+ []const []const u8 => TypeId.List,
+ else => @compileError("Unsupported type: " ++ @typeName(T)),
+ },
}
}
diff --git a/std/fmt.zig b/std/fmt.zig
index d1b91b15f8..a42233cac2 100644
--- a/std/fmt.zig
+++ b/std/fmt.zig
@@ -2,6 +2,7 @@ const math = @import("math.zig");
const debug = @import("debug.zig");
const assert = debug.assert;
const mem = @import("mem.zig");
+const builtin = @import("builtin");
const max_f64_digits = 65;
const max_int_digits = 65;
@@ -174,19 +175,25 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->bool,
pub fn formatValue(value: var, context: var, output: fn(@typeOf(context), []const u8)->bool) -> bool {
const T = @typeOf(value);
- if (@isInteger(T)) {
- return formatInt(value, 10, false, 0, context, output);
- } else if (@isFloat(T)) {
- @compileError("TODO implement formatFloat");
- } else if (@canImplicitCast([]const u8, value)) {
- const casted_value = ([]const u8)(value);
- return output(context, casted_value);
- } else if (T == void) {
- return output(context, "void");
- } else if (T == bool) {
- return output(context, if (value) "true" else "false");
- } else {
- @compileError("Unable to format type '" ++ @typeName(T) ++ "'");
+ switch (@typeId(T)) {
+ builtin.TypeId.Int => {
+ return formatInt(value, 10, false, 0, context, output);
+ },
+ builtin.TypeId.Float => {
+ @compileError("TODO implement formatFloat");
+ },
+ builtin.TypeId.Void => {
+ return output(context, "void");
+ },
+ builtin.TypeId.Bool => {
+ return output(context, if (value) "true" else "false");
+ },
+ else => if (@canImplicitCast([]const u8, value)) {
+ const casted_value = ([]const u8)(value);
+ return output(context, casted_value);
+ } else {
+ @compileError("Unable to format type '" ++ @typeName(T) ++ "'");
+ },
}
}
diff --git a/std/math.zig b/std/math.zig
index c146272d62..4941f4060c 100644
--- a/std/math.zig
+++ b/std/math.zig
@@ -1,4 +1,5 @@
const assert = @import("debug.zig").assert;
+const builtin = @import("builtin");
pub const Cmp = enum {
Less,
@@ -61,23 +62,27 @@ fn testOverflow() {
pub fn log(comptime base: usize, value: var) -> @typeOf(value) {
const T = @typeOf(value);
- if (@isInteger(T)) {
- if (base == 2) {
- return T.bit_count - 1 - @clz(value);
- } else {
- @compileError("TODO implement log for non base 2 integers");
- }
- } else if (@isFloat(T)) {
- @compileError("TODO implement log for floats");
- } else {
- @compileError("log expects integer or float, found '" ++ @typeName(T) ++ "'");
+ switch (@typeId(T)) {
+ builtin.TypeId.Int => {
+ if (base == 2) {
+ return T.bit_count - 1 - @clz(value);
+ } else {
+ @compileError("TODO implement log for non base 2 integers");
+ }
+ },
+ builtin.TypeId.Float => {
+ @compileError("TODO implement log for floats");
+ },
+ else => {
+ @compileError("log expects integer or float, found '" ++ @typeName(T) ++ "'");
+ },
}
}
error Overflow;
pub fn absInt(x: var) -> %@typeOf(x) {
const T = @typeOf(x);
- comptime assert(@isInteger(T)); // must pass an integer to absInt
+ comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
comptime assert(T.is_signed); // must pass a signed integer to absInt
if (x == @minValue(@typeOf(x)))
return error.Overflow;
@@ -97,7 +102,7 @@ fn testAbsInt() {
}
pub fn absFloat(x: var) -> @typeOf(x) {
- comptime assert(@isFloat(@typeOf(x)));
+ comptime assert(@typeId(@typeOf(x)) == builtin.TypeId.Float);
return if (x < 0) -x else x;
}
@@ -116,7 +121,7 @@ pub fn divTrunc(comptime T: type, numerator: T, denominator: T) -> %T {
@setDebugSafety(this, false);
if (denominator == 0)
return error.DivisionByZero;
- if (@isInteger(T) and T.is_signed and numerator == @minValue(T) and denominator == -1)
+ if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)
return error.Overflow;
return @divTrunc(numerator, denominator);
}
@@ -141,7 +146,7 @@ pub fn divFloor(comptime T: type, numerator: T, denominator: T) -> %T {
@setDebugSafety(this, false);
if (denominator == 0)
return error.DivisionByZero;
- if (@isInteger(T) and T.is_signed and numerator == @minValue(T) and denominator == -1)
+ if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)
return error.Overflow;
return @divFloor(numerator, denominator);
}
@@ -167,7 +172,7 @@ pub fn divExact(comptime T: type, numerator: T, denominator: T) -> %T {
@setDebugSafety(this, false);
if (denominator == 0)
return error.DivisionByZero;
- if (@isInteger(T) and T.is_signed and numerator == @minValue(T) and denominator == -1)
+ if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)
return error.Overflow;
const result = @divTrunc(numerator, denominator);
if (result * denominator != numerator)
@@ -246,7 +251,7 @@ fn testRem() {
}
fn isNan(comptime T: type, x: T) -> bool {
- assert(@isFloat(T));
+ assert(@typeId(T) == builtin.TypeId.Float);
const bits = floatBits(x);
if (T == f32) {
return (bits & 0x7fffffff) > 0x7f800000;
@@ -258,7 +263,7 @@ fn isNan(comptime T: type, x: T) -> bool {
}
fn floatBits(comptime T: type, x: T) -> @IntType(false, T.bit_count) {
- assert(@isFloat(T));
+ assert(@typeId(T) == builtin.TypeId.Float);
const uint = @IntType(false, T.bit_count);
return *@intToPtr(&const uint, &x);
}