mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-17 10:23:46 +00:00
Discussion of these changes can be found on the Mercury developers
mailing list archives from June 2018.
COPYING.LIB:
Add a special linking exception to the LGPL.
*:
Update references to COPYING.LIB.
Clean up some minor errors that have accumulated in copyright
messages.
102 lines
3.3 KiB
Mathematica
102 lines
3.3 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%---------------------------------------------------------------------------%
|
|
% Copyright (C) 2006-2007 The University of Melbourne.
|
|
% Copyright (C) 2014-2015, 2018 The Mercury team.
|
|
% This file is distributed under the terms specified in COPYING.LIB.
|
|
%---------------------------------------------------------------------------%
|
|
%
|
|
% File: string.builder.m.
|
|
% Main author: maclarty.
|
|
%
|
|
% This module implements a string builder stream. It can be used to
|
|
% build up a string using string or character writers.
|
|
%
|
|
% To build up a string using this module, you first construct an initial
|
|
% string builder state by calling the init function. You can then use
|
|
% any instances of stream.writer that write strings or characters to update the
|
|
% string builder state, using string.builder.handle as the stream argument.
|
|
% Once you've finished writing to the string builder you can get the final
|
|
% string by calling string.builder.to_string/1.
|
|
%
|
|
% For example:
|
|
%
|
|
% State0 = string.builder.init,
|
|
% stream.string_writer.put_int(string.builder.handle, 5, State0, State),
|
|
% Str = string.builder.to_string(State), % Str = "5".
|
|
%
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- module string.builder.
|
|
:- interface.
|
|
|
|
:- import_module char.
|
|
:- import_module stream.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- type handle
|
|
---> handle.
|
|
|
|
:- type state.
|
|
|
|
:- func init = (string.builder.state::uo) is det.
|
|
|
|
:- instance stream.stream(string.builder.handle, string.builder.state).
|
|
|
|
:- instance stream.output(string.builder.handle, string.builder.state).
|
|
|
|
:- instance stream.writer(string.builder.handle, string, string.builder.state).
|
|
:- instance stream.writer(string.builder.handle, char, string.builder.state).
|
|
|
|
:- func to_string(string.builder.state::di) = (string::uo) is det.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module list.
|
|
|
|
:- type state
|
|
---> state(list(string)).
|
|
|
|
init = state([]).
|
|
|
|
:- instance stream.stream(string.builder.handle, string.builder.state)
|
|
where [
|
|
name(_, "<<string builder stream>>", !State)
|
|
].
|
|
|
|
:- instance stream.output(string.builder.handle, string.builder.state)
|
|
where [
|
|
flush(_, !State)
|
|
].
|
|
|
|
:- instance stream.writer(string.builder.handle, string, string.builder.state)
|
|
where [
|
|
( put(_, String, !State) :-
|
|
!.State = state(StringList0),
|
|
copy(String, UniqueString),
|
|
StringList = [UniqueString | StringList0],
|
|
!:State = state(StringList)
|
|
)
|
|
].
|
|
|
|
:- instance stream.writer(string.builder.handle, char, string.builder.state)
|
|
where [
|
|
( put(_, Char, !State) :-
|
|
!.State = state(StringList0),
|
|
StringList = [string.from_char(Char) | StringList0],
|
|
!:State = state(StringList)
|
|
)
|
|
].
|
|
|
|
to_string(State) = String :-
|
|
State = state(StringList),
|
|
String = string.append_list(list.reverse(StringList)).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
:- end_module string.builder.
|
|
%---------------------------------------------------------------------------%
|