Code Monkey home page Code Monkey logo

stringviews.jl's Introduction

StringViews

CI

This Julia package implements a new type of AbstractString, a StringView, that provides a string representation of any underlying array of bytes (any AbstractVector{UInt8}), interpreted as UTF-8 encoded Unicode data.

Unlike Julia's built-in String type (which also wraps UTF-8 data), the StringView type is a copy-free wrap of any AbstractVector{UInt8} instance, and does not take "ownership" of or modify the array. You can also use StringView(buf) with a buf::IOBuffer, as a non-destructive alternative to String(take!(buf)). Otherwise, a StringView is intended to be usable in any context where you might have otherwise used String.

(In particular, as much as possible we try to implement efficient copy-free String-like operations on StringView, such as iteration and regular-expression searching, as long as the underlying UInt8 array is a contiguous dense array.)

For example:

julia> b = [0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72];

julia> s = StringView(b) # does not make a copy
"foobar"

julia> collect(eachmatch(r"[aeiou]+", s))
2-element Vector{SVRegexMatch{StringView{Vector{UInt8}}}}:
 SVRegexMatch("oo")
 SVRegexMatch("a")

julia> StringView(@view b[1:3]) # also works for subarrays, with no copy
"foo"

julia> abc = StringView(0x61:0x63) # and for other array types
"abc"

Or, with an IOBuffer:

julia> buf = IOBuffer();

julia> write(buf, b);

julia> print(buf, "baz")

julia> StringView(buf) # does not modify buf
"foobarbaz"

julia> String(take!(buf)) # clears buf
"foobarbaz"

julia> StringView(buf) # now empty
""

Other optimized (copy-free) operations include I/O, hashing, iteration/indexing, comparisons, parsing, searching, and validation. Working with a SubString of a StringView is similarly efficient.

stringviews.jl's People

Contributors

stevengj avatar kristofferc avatar jakobnissen avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.