Permalink
Browse files

some cleanup of strings and printing

remove special case print() of Vector{Uint8}. it was doing what write() is for.
make string() a general string constructor that prints all its
  arguments together into a string.
strcat() is now intended for concatenating strings only.
add sshow(x) to replace sprint(show, x)
string(x) can replace sprint(print, x)
  • Loading branch information...
JeffBezanson committed May 4, 2012
1 parent f4529b4 commit 103a05790230685b67df9273fd9a7e2467da0baa
Showing with 63 additions and 63 deletions.
  1. +1 −1 base/ascii.jl
  2. +2 −2 base/expr.jl
  3. +7 −1 base/io.jl
  4. +4 −4 base/math_libm.jl
  5. +1 −1 base/operators.jl
  6. +3 −3 base/printf.jl
  7. +1 −3 base/show.jl
  8. +5 −4 base/stage0.jl
  9. +23 −22 base/string.jl
  10. +1 −1 base/sysimg.jl
  11. +4 −4 base/utf8.jl
  12. +2 −2 base/util.jl
  13. +1 −1 base/version.jl
  14. +3 −3 extras/cpp.jl
  15. +1 −1 extras/dicom.jl
  16. +0 −5 src/builtins.c
  17. +1 −1 src/gf.c
  18. +0 −1 src/julia.expmap
  19. +3 −3 test/strings.jl
View
@@ -68,7 +68,7 @@ end
## outputing ASCII strings ##
-print(io::IO, s::ASCIIString) = print(io, s.data)
+print(io::IO, s::ASCIIString) = (write(io, s.data);nothing)
write(io, s::ASCIIString) = write(io, s.data)
## transcoding to ASCII ##
View
@@ -56,11 +56,11 @@ isequal(x::Symbol , y::SymbolNode) = is(x,y.name)
function show(io, tv::TypeVar)
if !is(tv.lb, None)
show(io, tv.lb)
- print(io, "<:".data)
+ print(io, "<:")
end
print(io, tv.name)
if !is(tv.ub, Any)
- print(io, "<:".data)
+ print(io, "<:")
show(io, tv.ub)
end
end
View
@@ -39,7 +39,7 @@ function fdio(name::String, fd::Integer, own::Bool)
return s
end
fdio(name::String, fd::Integer) = fdio(name, fd, false)
-fdio(fd::Integer, own::Bool) = fdio(strcat("<fd ",fd,">"), fd, own)
+fdio(fd::Integer, own::Bool) = fdio(string("<fd ",fd,">"), fd, own)
fdio(fd::Integer) = fdio(fd, false)
make_stdin_stream() = fdio("<stdin>", ccall(:jl_stdin, Int32, ()))
@@ -94,6 +94,12 @@ end
sprint(f::Function, args...) = sprint(0, f, args...)
+function sshow(x)
+ s = memio(0, false)
+ show(s, x)
+ takebuf_string(s)
+end
+
# using this is not recommended
function with_output_to_string(thunk)
global OUTPUT_STREAM
View
@@ -3,7 +3,7 @@ _jl_libm = dlopen("libm")
macro _jl_libmfunc_1arg_float(T,f)
quote
($f)(x::Float64) = ccall(dlsym(_jl_libm,$string(f)), Float64, (Float64,), x)
- ($f)(x::Float32) = ccall(dlsym(_jl_libm,$strcat(string(f),"f")), Float32, (Float32,), x)
+ ($f)(x::Float32) = ccall(dlsym(_jl_libm,$string(f,"f")), Float32, (Float32,), x)
($f)(x::Real) = ($f)(float(x))
@vectorize_1arg $T $f
end
@@ -12,7 +12,7 @@ end
macro _jl_libfdmfunc_1arg_float(T,f)
quote
($f)(x::Float64) = ccall(dlsym(_jl_libfdm,$string(f)), Float64, (Float64,), x)
- ($f)(x::Float32) = ccall(dlsym(_jl_libfdm,$strcat(string(f),"f")), Float32, (Float32,), x)
+ ($f)(x::Float32) = ccall(dlsym(_jl_libfdm,$string(f,"f")), Float32, (Float32,), x)
($f)(x::Real) = ($f)(float(x))
@vectorize_1arg $T $f
end
@@ -26,15 +26,15 @@ macro _jl_libmfunc_1arg_int(T,f,name...)
end
quote
($fname)(x::Float64) = ccall(dlsym(_jl_libm,$string(f)), Int32, (Float64,), x)
- ($fname)(x::Float32) = ccall(dlsym(_jl_libm,$strcat(string(f),"f")), Int32, (Float32,), x)
+ ($fname)(x::Float32) = ccall(dlsym(_jl_libm,$string(f,"f")), Int32, (Float32,), x)
@vectorize_1arg $T $fname
end
end
macro _jl_libfdmfunc_2arg(T,f)
quote
($f)(x::Float64, y::Float64) = ccall(dlsym(_jl_libfdm,$string(f)), Float64, (Float64, Float64,), x, y)
- ($f)(x::Float32, y::Float32) = ccall(dlsym(_jl_libfdm,$strcat(string(f),"f")), Float32, (Float32, Float32), x, y)
+ ($f)(x::Float32, y::Float32) = ccall(dlsym(_jl_libfdm,$string(f,"f")), Float32, (Float32, Float32), x, y)
@vectorize_2arg $T $f
end
end
View
@@ -97,7 +97,7 @@ oftype{T}(x::T,c) = convert(T,c)
zero(x) = oftype(x,0)
one(x) = oftype(x,1)
-sizeof(T::Type) = error(strcat("size of type ",T," unknown"))
+sizeof(T::Type) = error(string("size of type ",T," unknown"))
sizeof(T::BitsKind) = div(T.nbits,8)
sizeof{T}(x::T) = sizeof(T)
View
@@ -75,7 +75,7 @@ end
# [diouxXeEfFgGaAcCsSp%] # conversion
_jl_next_or_die(s::String, k) = !done(s,k) ? next(s,k) :
- error("invalid printf format string: ", sprint(show, s))
+ error("invalid printf format string: ", sshow(s))
function _jl_printf_parse1(s::String, k::Integer)
j = k
@@ -119,7 +119,7 @@ function _jl_printf_parse1(s::String, k::Integer)
end
# validate conversion
if !contains("diouxXDOUeEfFgGaAcCsSpn", c)
- error("invalid printf format string: ", sprint(show, s))
+ error("invalid printf format string: ", sshow(s))
end
# TODO: warn about silly flag/conversion combinations
flags, width, precision, c, k
@@ -450,7 +450,7 @@ function _jl_printf_s(flags::ASCIIString, width::Int, precision::Int, c::Char)
if !contains(flags,'#')
push(blk.args, :($x = string($x)))
else
- push(blk.args, :($x = sprint(show, $x)))
+ push(blk.args, :($x = sshow($x)))
end
if !contains(flags,'-')
push(blk.args, _jl_printf_pad(width, :($width-strwidth($x)), ' '))
View
@@ -2,8 +2,6 @@
show(x) = show(OUTPUT_STREAM::IOStream, x)
-print(io::IOStream, a::Array{Uint8,1}) =
- ccall(:jl_print_array_uint8, Void, (Ptr{Void}, Any,), io, a)
print(io::IOStream, s::Symbol) = ccall(:jl_print_symbol, Void, (Ptr{Void}, Any,), io, s)
show(io, x) = ccall(:jl_show_any, Void, (Any, Any,), io, x)
@@ -405,7 +403,7 @@ dims2string(d) = length(d) == 0 ? "0-dimensional" :
join(map(string,d), 'x')
summary{T}(a::AbstractArray{T}) =
- strcat(dims2string(size(a)), " ", string(T), " ", string(typeof(a).name))
+ string(dims2string(size(a)), " ", T, " ", typeof(a).name)
function show_nd(io, a::AbstractArray)
if isempty(a)
View
@@ -6,15 +6,16 @@ if false
length(a::Array) = arraylen(a)
print(x) = print(stdout_stream, x)
show(x) = show(stdout_stream, x)
- print(io, a::Array{Uint8,1}) =
- ccall(:jl_print_array_uint8, Void, (Ptr{Void},Any,), io.ios, a)
+ write(io, a::Array{Uint8,1}) =
+ ccall(:ios_write, Uint, (Ptr{Void}, Ptr{Void}, Uint),
+ io.ios, a, length(a))
print(io, s::Symbol) = ccall(:jl_print_symbol, Void, (Ptr{Void},Any,),
io.ios, s)
- print(io, s::ASCIIString) = print(io, s.data)
+ print(io, s::ASCIIString) = (write(io, s.data);nothing)
print(io, x) = show(io, x)
println(io, x) = (print(io, x); print(io, "\n"))
show(io, x) = ccall(:jl_show_any, Void, (Any, Any,), io, x)
- show(io, s::ASCIIString) = print(io, s.data)
+ show(io, s::ASCIIString) = (write(io, s.data);nothing)
show(io, s::Symbol) = print(io, s)
show(io, b::Bool) = print(io, b ? "true" : "false")
show(io, n::Int64) = ccall(:jl_print_int64, Void, (Ptr{Void}, Int64,), io, n)
View
@@ -25,12 +25,13 @@ ref(s::String, x::Real) = s[iround(x)]
ref{T<:Integer}(s::String, r::Range1{T}) = s[int(first(r)):int(last(r))]
# TODO: handle other ranges with stride ±1 specially?
ref(s::String, v::AbstractVector) =
- sprint(length(v), io->(for i in v print(io,s[i]) end))
+ sprint(length(v), io->(for i in v write(io,s[i]) end))
symbol(s::String) = symbol(cstring(s))
+string() = ""
string(s::String) = s
-print(io::IO, s::String) = for c in s print(io, c) end
+print(io::IO, s::String) = for c in s write(io, c) end
show(io::IO, s::String) = print_quoted(io, s)
(*)(s::String...) = strcat(s...)
@@ -252,15 +253,6 @@ next(s::CharString, i::Int) = (s.chars[i], i+1)
length(s::CharString) = length(s.chars)
strlen(s::CharString) = length(s)
-function string(c::Char, x::Char...)
- s = memio(1+length(x), false)
- write(s, c)
- for ch in x
- write(s, ch)
- end
- takebuf_string(s)
-end
-
## substrings reference original strings ##
type SubString{T<:String} <: String
@@ -386,9 +378,9 @@ strlen(s::RopeString) = strlen(s.head) + strlen(s.tail)
strcat() = ""
strcat(s::String) = s
-strcat(x...) = strcat(map(string,x)...)
strcat(s::String, t::String...) =
(t = strcat(t...); isempty(s) ? t : isempty(t) ? s : RopeString(s, t))
+strcat(xs...) = string(xs...) # backwards compat
print(io::IO, s::RopeString) = print(io, s.head, s.tail)
@@ -434,8 +426,17 @@ end
## conversion of general objects to strings ##
-string(x) = sprint(show, x)
-cstring(x...) = sprint(print, x...)
+cstring() = ""
+
+function cstring(xs...)
+ s = memio(isa(xs[1],String) ? length(xs[1]) : 0, false)
+ for x in xs
+ print(s, x)
+ end
+ takebuf_string(s)
+end
+
+string(xs...) = cstring(xs...)
function cstring(p::Ptr{Uint8})
p == C_NULL ? error("cannot convert NULL to string") :
@@ -497,7 +498,7 @@ quote_string(s::String) = sprint(length(s)+2, io->print_quoted(io,s))
function print_unescaped_chars(io, s::String, esc::String)
if !contains(esc,'\\')
- esc = strcat("\\", esc)
+ esc = string("\\", esc)
end
i = start(s)
while !done(s,i)
@@ -734,7 +735,7 @@ function shell_split(s::String)
parsed = _jl_shell_parse(s,false)
args = String[]
for arg in parsed
- push(args, strcat(arg...))
+ push(args, string(arg...))
end
args
end
@@ -878,7 +879,7 @@ function replace(str::ByteString, splitter, repl::Function, limit::Integer)
n += 1
end
rstr = RopeString(rstr,SubString(str,i))
- sprint(length(rstr),print,rstr)
+ cstring(rstr)
end
replace(s::String, spl, f::Function, n::Integer) = replace(cstring(s), spl, f, n)
replace(s::String, spl, r, n::Integer) = replace(s, spl, x->r, n)
@@ -951,7 +952,7 @@ function parse_int{T<:Integer}(::Type{T}, s::String, base::Integer)
while true
if done(s,i)
throw(ArgumentError(strcat(
- "premature end of integer (in ", sprint(show, s) ,")"
+ "premature end of integer (in ", sshow(s) ,")"
)))
end
c,i = next(s,i)
@@ -964,14 +965,14 @@ function parse_int{T<:Integer}(::Type{T}, s::String, base::Integer)
sgn = -sgn
if done(s,i)
throw(ArgumentError(strcat(
- "premature end of integer (in ", sprint(show, s), ")"
+ "premature end of integer (in ", sshow(s), ")"
)))
end
c,i = next(s,i)
elseif c == '+'
if done(s,i)
throw(ArgumentError(strcat(
- "premature end of integer (in ", sprint(show, s), ")"
+ "premature end of integer (in ", sshow(s), ")"
)))
end
c,i = next(s,i)
@@ -985,14 +986,14 @@ function parse_int{T<:Integer}(::Type{T}, s::String, base::Integer)
if d >= base
if !iswspace(c)
throw(ArgumentError(strcat(
- sprint(show, c)," is not a valid digit (in ", sprint(show, s), ")"
+ sshow(c)," is not a valid digit (in ", sshow(s), ")"
)))
end
while !done(s,i)
c,i = next(s,i)
if !iswspace(c)
throw(ArgumentError(strcat(
- "extra characters after whitespace (in ", sprint(show, s), ")"
+ "extra characters after whitespace (in ", sshow(s), ")"
)))
end
end
View
@@ -38,9 +38,9 @@ include("inference.jl")
# I/O, strings & printing
include("io.jl")
include("char.jl")
-include("string.jl")
include("ascii.jl")
include("utf8.jl")
+include("string.jl")
include("regex.jl")
include("show.jl")
include("grisu.jl")
View
@@ -82,17 +82,17 @@ strcat(a::ByteString, b::ByteString, c::ByteString...) = UTF8String(memcat(a,b,c
# ^^ at least one must be UTF-8 or the ASCII-only method would get called
transform_to_utf8(s::String, f::Function) =
- sprint(length(s), io->for c=s; print(io,f(c)); end)
+ sprint(length(s), io->for c in s; write(io,f(c)::Char); end)
uppercase(s::UTF8String) = transform_to_utf8(s, uppercase)
lowercase(s::UTF8String) = transform_to_utf8(s, lowercase)
-ucfirst(s::UTF8String) = sprint(length(s), io->print(io, uppercase(s[1]), s[2:]))
-lcfirst(s::UTF8String) = sprint(length(s), io->print(io, lowercase(s[1]), s[2:]))
+ucfirst(s::UTF8String) = string(uppercase(s[1]), s[2:])
+lcfirst(s::UTF8String) = string(lowercase(s[1]), s[2:])
## outputing UTF-8 strings ##
-print(io::IO, s::UTF8String) = print(io, s.data)
+print(io::IO, s::UTF8String) = (write(io, s.data);nothing)
write(io, s::UTF8String) = write(io, s.data)
## transcoding to UTF-8 ##
View
@@ -103,7 +103,7 @@ function edit(file::String, line::Int)
elseif editor == "subl"
run(`subl $file:$line`)
else
- error("Invalid JULIA_EDITOR value: $(sprint(show, editor))")
+ error("Invalid JULIA_EDITOR value: $(sshow(editor))")
end
else
if editor == "emacs"
@@ -115,7 +115,7 @@ function edit(file::String, line::Int)
elseif editor == "subl"
run(`subl $file:$line`)
else
- error("Invalid JULIA_EDITOR value: $(sprint(show, editor))")
+ error("Invalid JULIA_EDITOR value: $(sshow(editor))")
end
end
nothing
View
@@ -132,7 +132,7 @@ end
const VERSION = convert(VersionNumber,chomp(readall(open("$JULIA_HOME/VERSION"))))
try
- ver = sprint(print,VERSION)
+ ver = string(VERSION)
commit = chomp(readall(`git rev-parse HEAD`))
tagged = try chomp(readall(`git rev-parse --verify --quiet v$ver`))
catch "doesn't reference a commit"; end
View
@@ -21,7 +21,7 @@ macro cpp(ex)
fstr = string(sym)
fstr = fstr[2:end] # strip the :
#GNU3-4 ABI
- fstr = strcat("_Z",strlen(fstr),fstr)
+ fstr = string("_Z",strlen(fstr),fstr)
# Parse the arguments to ccall and construct the parameter type string
extmp = ex.args[3]
if extmp.head != :tuple
@@ -35,14 +35,14 @@ macro cpp(ex)
for iarg = 1:length(exargs)
thisarg = exargs[iarg]
if isa(thisarg,Expr) && thisarg.head == :curly && thisarg.args[1] == :Ptr
- pstr = strcat(pstr,'P')
+ pstr = string(pstr,'P')
thisarg = thisarg.args[2]
end
matched = false
for isym = 1:length(symtable)
if thisarg == symtable[isym]
matched = true
- pstr = strcat(pstr,ptable[isym])
+ pstr = string(pstr,ptable[isym])
# Cchar is a special notation just for name mangling,
# convert back to :Int8
if thisarg == :Cchar
View
@@ -229,7 +229,7 @@ function string_parse(st, sz, maxlen, spaces)
push(data, "")
first = true
else
- data[end] = strcat(data[end],c) # TODO: inefficient
+ data[end] = string(data[end],c) # TODO: inefficient
first = false
end
end
View
@@ -711,11 +711,6 @@ DLLEXPORT void *jl_array_ptr(jl_array_t *a)
// printing -------------------------------------------------------------------
-DLLEXPORT void jl_print_array_uint8(ios_t *s, jl_array_t *b)
-{
- ios_write(s, (char*)b->data, b->length);
-}
-
DLLEXPORT void jl_print_symbol(ios_t *s, jl_sym_t *sym)
{
ios_puts(sym->name, s);
Oops, something went wrong.

0 comments on commit 103a057

Please sign in to comment.