Skip to content

Commit 3653015

Browse files
committed
---
yaml --- r: 11672 b: refs/heads/master c: e957185 h: refs/heads/master v: v3
1 parent 781b3c2 commit 3653015

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 0e3dd5a3ee170482f8e314b3f37aa2d1cc8da58c
2+
refs/heads/master: e9571850daded0f46ae3b9b8e0834b6be2e5219b
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/src/libcore/libc.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,10 +1100,11 @@ mod funcs {
11001100

11011101
#[abi = "stdcall"]
11021102
native mod kernel32 {
1103-
fn GetEnvironmentVariableA(n: LPCSTR,
1104-
v: LPSTR,
1103+
fn GetEnvironmentVariableW(n: LPCWSTR,
1104+
v: LPWSTR,
11051105
nsize: DWORD) -> DWORD;
1106-
fn SetEnvironmentVariableA(n: LPCSTR, v: LPCSTR) -> BOOL;
1106+
fn SetEnvironmentVariableW(n: LPCWSTR, v: LPCWSTR) -> BOOL;
1107+
11071108
fn GetModuleFileNameA(hModule: HMODULE,
11081109
lpFilename: LPSTR,
11091110
nSize: DWORD) -> DWORD;

trunk/src/libcore/os.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,17 @@ fn as_c_charp<T>(s: str, f: fn(*c_char) -> T) -> T {
5050
str::as_buf(s) {|b| f(b as *c_char) }
5151
}
5252

53-
// FIXME: UTF-16-ify this and revive win32 Unicode variant.
53+
fn as_utf16_p<T>(s: str, f: fn(*u16) -> T) -> T {
54+
let t = str::to_utf16(s);
55+
// "null terminate"
56+
t += [0u16];
57+
vec::as_buf(t, f)
58+
}
59+
60+
61+
#[cfg(target_os = "linux")]
62+
#[cfg(target_os = "macos")]
63+
#[cfg(target_os = "freebsd")]
5464
fn getenv(n: str) -> option<str> unsafe {
5565
let s = as_c_charp(n, libc::getenv);
5666
ret if unsafe::reinterpret_cast(s) == 0 {
@@ -61,6 +71,27 @@ fn getenv(n: str) -> option<str> unsafe {
6171
};
6272
}
6373

74+
#[cfg(target_os = "win32")]
75+
fn getenv(n: str) -> option<str> unsafe {
76+
import libc::types::os::arch::extra::*;
77+
import libc::funcs::extra::kernel32;
78+
as_utf16_p(n) {|u|
79+
let bufsize = 1023u;
80+
let buf = vec::to_mut(vec::init_elt(bufsize, 0u16));
81+
vec::as_mut_buf(buf) {|b|
82+
let k = kernel32::GetEnvironmentVariableW(u, b,
83+
bufsize as DWORD);
84+
if k != (0 as DWORD) {
85+
let sub = vec::slice(buf, 0u, k as uint);
86+
option::some::<str>(str::from_utf16(sub))
87+
} else {
88+
option::none::<str>
89+
}
90+
}
91+
}
92+
}
93+
94+
6495
#[cfg(target_os = "linux")]
6596
#[cfg(target_os = "macos")]
6697
#[cfg(target_os = "freebsd")]
@@ -78,13 +109,12 @@ fn setenv(n: str, v: str) {
78109

79110

80111
#[cfg(target_os = "win32")]
81-
// FIXME: UTF-16-ify this and switch to the W version.
82112
fn setenv(n: str, v: str) {
83113
// FIXME: remove imports when export globs work properly.
84114
import libc::funcs::extra::kernel32;
85-
as_c_charp(n) {|nbuf|
86-
as_c_charp(v) {|vbuf|
87-
kernel32::SetEnvironmentVariableA(nbuf, vbuf);
115+
as_utf16_p(n) {|nbuf|
116+
as_utf16_p(v) {|vbuf|
117+
kernel32::SetEnvironmentVariableW(nbuf, vbuf);
88118
}
89119
}
90120
}

trunk/src/libcore/str.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ Function: is_utf8
10471047
10481048
Determines if a vector of bytes contains valid UTF-8
10491049
*/
1050-
fn is_utf8(v: [u8]) -> bool {
1050+
fn is_utf8(v: [const u8]) -> bool {
10511051
let i = 0u;
10521052
let total = vec::len::<u8>(v);
10531053
while i < total {
@@ -1065,7 +1065,7 @@ fn is_utf8(v: [u8]) -> bool {
10651065
}
10661066

10671067

1068-
fn is_utf16(v: [u16]) -> bool {
1068+
fn is_utf16(v: [const u16]) -> bool {
10691069
let len = v.len();
10701070
let i = 0u;
10711071
while (i < len) {
@@ -1108,10 +1108,10 @@ fn to_utf16(s: str) -> [u16] {
11081108
ret u;
11091109
}
11101110

1111-
fn utf16_chars(v: [u16], f: fn(char)) {
1111+
fn utf16_chars(v: [const u16], f: fn(char)) {
11121112
let len = v.len();
11131113
let i = 0u;
1114-
while (i < len) {
1114+
while (i < len && v[i] != 0u16) {
11151115
let u = v[i];
11161116

11171117
if u <= 0xD7FF_u16 || u >= 0xE000_u16 {
@@ -1133,7 +1133,7 @@ fn utf16_chars(v: [u16], f: fn(char)) {
11331133
}
11341134

11351135

1136-
fn from_utf16(v: [u16]) -> str {
1136+
fn from_utf16(v: [const u16]) -> str {
11371137
let buf = "";
11381138
reserve(buf, v.len());
11391139
utf16_chars(v) {|ch| push_char(buf, ch); }

0 commit comments

Comments
 (0)