Commit f6c77746d6

Andrew Kelley <superjoe30@gmail.com>
2018-04-11 04:24:01
add memmove to builtin.o
related: #514
1 parent 27e881c
Changed files (1)
std
special
std/special/builtin.zig
@@ -14,26 +14,43 @@ pub fn panic(msg: []const u8, error_return_trace: ?&builtin.StackTrace) noreturn
     }
 }
 
-// Note that memset does not return `dest`, like the libc API.
-// The semantics of memset is dictated by the corresponding
-// LLVM intrinsics, not by the libc API.
-export fn memset(dest: ?&u8, c: u8, n: usize) void {
+export fn memset(dest: ?&u8, c: u8, n: usize) ?&u8 {
     @setRuntimeSafety(false);
 
     var index: usize = 0;
     while (index != n) : (index += 1)
         (??dest)[index] = c;
+
+    return dest;
 }
 
-// Note that memcpy does not return `dest`, like the libc API.
-// The semantics of memcpy is dictated by the corresponding
-// LLVM intrinsics, not by the libc API.
-export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) void {
+export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) ?&u8 {
     @setRuntimeSafety(false);
 
     var index: usize = 0;
     while (index != n) : (index += 1)
         (??dest)[index] = (??src)[index];
+
+    return dest;
+}
+
+export fn memmove(dest: ?&u8, src: ?&const u8, n: usize) ?&u8 {
+    @setRuntimeSafety(false);
+
+    if (@ptrToInt(dest) < @ptrToInt(src)) {
+        var index: usize = 0;
+        while (index != n) : (index += 1) {
+            (??dest)[index] = (??src)[index];
+        }
+    } else {
+        var index = n;
+        while (index != 0) {
+            index -= 1;
+            (??dest)[index] = (??src)[index];
+        }
+    }
+
+    return dest;
 }
 
 comptime {