Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/JuliaLang/julia
Browse files Browse the repository at this point in the history
  • Loading branch information
HarlanH committed Sep 4, 2012
2 parents 5c39d80 + fc1917a commit 6b034b8
Show file tree
Hide file tree
Showing 28 changed files with 2,029 additions and 110 deletions.
2 changes: 1 addition & 1 deletion base/ascii.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ end
## outputing ASCII strings ##

print(io::IO, s::ASCIIString) = (write(io, s.data);nothing)
write(io, s::ASCIIString) = write(io, s.data)
write(io::IO, s::ASCIIString) = write(io, s.data)

## transcoding to ASCII ##

Expand Down
70 changes: 70 additions & 0 deletions base/deepcopy.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# deep copying
deepcopy(x) = _deepcopy(x, ObjectIdDict())

_deepcopy(x::Union(Symbol,Function,LambdaStaticData,
TopNode,QuoteNode,BitsKind,CompositeKind,AbstractKind,
UnionKind), stackdict::ObjectIdDict) = x
_deepcopy(x::Tuple, stackdict::ObjectIdDict) =
ntuple(length(x), i->_deepcopy(x[i], stackdict))
_deepcopy(x::Module, stackdict::ObjectIdDict) = error("deepcopy of Modules not supported")

function _deepcopy(x, stackdict::ObjectIdDict)
if has(stackdict, x)
return stackdict[x]
end
_deepcopy_t(x, typeof(x), stackdict)
end

_deepcopy_t(x, T::BitsKind, stackdict::ObjectIdDict) = x
function _deepcopy_t(x, T::CompositeKind, stackdict::ObjectIdDict)
nf = length(T.names)

ret = ccall(:jl_new_struct_uninit, Any, (Any,), T)
stackdict[x] = ret
for i=1:nf
try
ccall(:jl_set_nth_field, Any, (Any, Int, Any),
ret, i-1, _deepcopy(x.(T.names[i]), stackdict))
catch err
# we ignore undefined references errors
if !isa(err, UndefRefError)
throw(err)
end
end
end
return ret
end
_deepcopy_t(x, T, stackdict::ObjectIdDict) =
error("deepcopy of objects of type ", T, " not supported")


function _deepcopy(x::Array, stackdict::ObjectIdDict)
if has(stackdict, x)
return stackdict[x]
end
_deepcopy_array_t(x, eltype(x), stackdict)
end

_deepcopy_array_t(x, T::BitsKind, stackdict::ObjectIdDict) = copy(x)
function _deepcopy_array_t(x, T, stackdict::ObjectIdDict)
dest = similar(x)
stackdict[x] = dest
i0 = 1; local i
while true
try
for i=i0:length(x)
# NOTE: this works around the performance problem caused by all
# the doubled definitions of assign()
arrayset(dest, i, _deepcopy(x[i], stackdict))
end
break
catch err
# we ignore undefined references errors
if !isa(err, UndefRefError)
throw(err)
end
i0 = i+1
end
end
return dest
end
1 change: 1 addition & 0 deletions base/export.jl
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@ export

# object identity and equality
copy,
deepcopy,
isequal,
isless,
identity,
Expand Down
6 changes: 3 additions & 3 deletions base/printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ function _special_handler(flags::ASCIIString, width::Int)
pos = contains(flags,'+') ? "+" :
contains(flags,' ') ? " " : ""
abn = quote
isnan($x) ? $(bytestring(pad("NaN", width))) :
$x < 0 ? $(bytestring(pad("-Inf", width))) :
$(bytestring(pad("$(pos)Inf", width)))
isnan($x) ? $(pad("NaN", width)) :
$x < 0 ? $(pad("-Inf", width)) :
$(pad("$(pos)Inf", width))
end
ex = :(isfinite($x) ? $blk : write(out, $abn))
x, ex, blk
Expand Down
3 changes: 2 additions & 1 deletion base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ function search(str::ByteString, re::Regex, idx::Integer)
m, n = PCRE.exec(re.regex, re.extra, str, idx-1, opts, true)
isempty(m) ? (0,0) : (m[1]+1,m[2]+1)
end
search(s::ByteString, r::Regex) = search(s,r,start(s))
search(s::String, r::Regex, idx::Integer) = error("regex search is only available for bytestrings; use bytestring(s) to convert")
search(s::String, r::Regex) = search(s,r,start(s))

type RegexMatchIterator
regex::Regex
Expand Down
1 change: 1 addition & 0 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ elements(s::Set) = keys(s.hash)
eltype{T}(s::Set{T}) = T

has(s::Set, x) = has(s.hash, x)
contains(s::Set, x) = has(s, x)
get(s::Set, x, deflt) = get(s.hash, x, false)

add(s::Set, x) = (s.hash[x] = true; s)
Expand Down
69 changes: 60 additions & 9 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ ref(s::String, v::AbstractVector) =
symbol(s::String) = symbol(bytestring(s))

print(io::IO, s::String) = for c in s write(io, c) end
write(io::IO, s::String) = print(io, s)
show(io::IO, s::String) = print_quoted(io, s)

(*)(s::String...) = strcat(s...)
Expand Down Expand Up @@ -156,7 +157,7 @@ function chr2ind(s::String, i::Integer)
end
end

typealias Chars Union(Char,AbstractVector{Char})
typealias Chars Union(Char,AbstractVector{Char},Set{Char})

function strchr(s::String, c::Chars, i::Integer)
if i < 1 error("index out of range") end
Expand All @@ -174,7 +175,15 @@ strchr(s::String, c::Chars) = strchr(s,c,start(s))

contains(s::String, c::Char) = (strchr(s,c)!=0)

search(s::String, c::Chars, i::Integer) = (i=strchr(s,c,i); (i,nextind(s,i)))
function search(s::String, c::Chars, i::Integer)
if isempty(c)
return 1 <= i <= length(s)+1 ? (i,i) :
i == length(s)+2 ? (0,0) :
error("index out of range")
end
i=strchr(s,c,i)
(i, nextind(s,i))
end
search(s::String, c::Chars) = search(s,c,start(s))

function search(s::String, t::String, i::Integer)
Expand Down Expand Up @@ -447,6 +456,8 @@ strcat(xs...) = string(xs...) # backwards compat

print(io::IO, s::RopeString) = print(io, s.head, s.tail)

write(io::IO, s::RopeString) = (write(io, s.head); write(io, s.tail))

## transformed strings ##

type TransformedString <: String
Expand All @@ -465,17 +476,57 @@ end

## uppercase and lowercase transformations ##

const _TF_U = (c,i)->uppercase(c)
const _TF_L = (c,i)->lowercase(c)
const _TF_u = (c,i)->i==1 ? uppercase(c) : c
const _TF_l = (c,i)->i==1 ? lowercase(c) : c
const _TF_C = (c,i)->i==1 ? uppercase(c) : lowercase(c)
const _TF_c = (c,i)->i==1 ? lowercase(c) : uppercase(c)

uppercase(c::Char) = ccall(:towupper, Char, (Char,), c)
lowercase(c::Char) = ccall(:towlower, Char, (Char,), c)

uppercase(c::Uint8) = ccall(:toupper, Uint8, (Uint8,), c)
lowercase(c::Uint8) = ccall(:tolower, Uint8, (Uint8,), c)

uppercase(s::String) = TransformedString((c,i)->uppercase(c), s)
lowercase(s::String) = TransformedString((c,i)->lowercase(c), s)
uppercase(s::String) = TransformedString(_TF_U, s)
lowercase(s::String) = TransformedString(_TF_L, s)

ucfirst(s::String) = TransformedString((c,i)->i==1 ? uppercase(c) : c, s)
lcfirst(s::String) = TransformedString((c,i)->i==1 ? lowercase(c) : c, s)
ucfirst(s::String) = TransformedString(_TF_u, s)
lcfirst(s::String) = TransformedString(_TF_l, s)

function _transfunc_compose(f2::Function, f1::Function)
allf = [_TF_U, _TF_L, _TF_u, _TF_l, _TF_C, _TF_c]
if !contains(allf, f2) || !contains(allf, f1)
return nothing
end
if f2 == _TF_U || f2 == _TF_L || f2 == _TF_C || f2 == _TF_c ||
f2 == f1 ||
(f2 == _TF_u && f1 == _TF_l) ||
(f2 == _TF_l && f1 == _TF_u)
return f2
elseif (f2 == _TF_u && (f1 == _TF_U || f1 == _TF_C)) ||
(f2 == _TF_l && (f1 == _TF_L || f1 == _TF_c))
return f1
elseif (f2 == _TF_u && f1 == _TF_L)
return _TF_C
elseif (f2 == _TF_l && f1 == _TF_U)
return _TF_c
elseif (f2 == _TF_u && f1 == _TF_c)
return _TF_U
elseif (f2 == _TF_l && f1 == _TF_C)
return _TF_L
end
error("this is a bug")
end

function TransformedString(transform::Function, s::TransformedString)
newtf = _transfunc_compose(transform, s.transform)
if newtf === nothing
return invoke(TransformedString, (Function, String), transform, s)
end
TransformedString(newtf, s.string)
end

const uc = uppercase
const lc = lowercase
Expand All @@ -500,7 +551,7 @@ function filter(f::Function, s::String)
takebuf_string(out)
end

has(s::String, c::Char) = has(Set(s...), c)
has(s::String, c::Char) = contains(s, c)

## string promotion rules ##

Expand Down Expand Up @@ -864,7 +915,7 @@ function lpad(s::String, n::Integer, p::String)
if m <= 0; return s; end
l = strlen(p)
if l==1
return p^m * s
return bytestring(p^m * s)
end
q = div(m,l)
r = m - q*l
Expand All @@ -876,7 +927,7 @@ function rpad(s::String, n::Integer, p::String)
if m <= 0; return s; end
l = strlen(p)
if l==1
return s * p^m
return bytestring(s * p^m)
end
q = div(m,l)
r = m - q*l
Expand Down
1 change: 1 addition & 0 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ include("mmap.jl")
include("version.jl")
include("util.jl")
include("datafmt.jl")
include("deepcopy.jl")

## Load optional external libraries

Expand Down
2 changes: 1 addition & 1 deletion base/utf8.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ lcfirst(s::UTF8String) = string(lowercase(s[1]), s[2:])
## outputing UTF-8 strings ##

print(io::IO, s::UTF8String) = (write(io, s.data);nothing)
write(io, s::UTF8String) = write(io, s.data)
write(io::IO, s::UTF8String) = write(io, s.data)

## transcoding to UTF-8 ##

Expand Down
16 changes: 16 additions & 0 deletions deps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,22 @@ clean-zlib:
distclean-zlib:
rm -rf zlib-$(ZLIB_VER).tar.gz zlib-$(ZLIB_VER)

## Tk wrapper ##

TKW_INC = -I $(USRINC) -I $(JULIAHOME)/src -I $(JULIAHOME)/src/support
TKW_LIB = -ltcl8.5 -ltk8.5 -L$(USRLIB)/ -ljulia-release

$(USRLIB)/libtk_wrapper.$(SHLIB_EXT): tk_wrapper.c
mkdir -p $(USRLIB)
$(CC) $(CFLAGS) $(LDFLAGS) -O2 -shared $(fPIC) $(TKW_INC) tk_wrapper.c $(TKW_LIB) -o $(USRLIB)/libtk_wrapper.$(SHLIB_EXT) -Wl,-rpath,$(USRLIB)
$(INSTALL_NAME_CMD)libtk_wrapper.$(SHLIB_EXT) $@
touch $@
install-tk-wrapper: $(USRLIB)/libtk_wrapper.$(SHLIB_EXT) tk_wrapper.c

clean-tk-wrapper:
rm -f $(USRLIB)/libtk_wrapper.$(SHLIB_EXT)
distclean-tk-wrapper: clean-tk-wrapper

## phony targets ##

.PHONY: \
Expand Down
27 changes: 27 additions & 0 deletions deps/tk_wrapper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdlib.h>
#include <tcl.h>
#include "julia.h"

int jl_tcl_callback(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
jl_function_t *f = (jl_function_t*)clientData;
jl_value_t **jlargs = alloca(argc * sizeof(void*));
memset(jlargs, 0, argc * sizeof(void*));
JL_GC_PUSHARGS(jlargs, argc);
int i;
for(i=0; i < argc; i++) {
jlargs[i] = jl_cstr_to_string(argv[i]);
}
jl_value_t *result = NULL;
JL_TRY {
result = jl_apply(f, jlargs, argc);
}
JL_CATCH {
JL_GC_POP();
return TCL_ERROR;
}
Tcl_SetResult(interp, jl_string_data(result), TCL_VOLATILE);
JL_GC_POP();
return TCL_OK;
}
Loading

0 comments on commit 6b034b8

Please sign in to comment.