tango.text.Text 翻译

 

tango.text.Text(r4774)

License:

BSD style: see license.txt

Version:

Initial release: December 2005

Author:

Kris

Text是一个管理和操作Unicode字符数组的类。

Text保持一个当前“选择对象”(selection),通过select()search()方法控制。append()prepend()replace()remove()中的每一个操作都是针对选择对象进行的。(这些方法分别是附加、前置、替换和移除)

search()方法(搜索)也可以针对当前选择对象进行操作,只要有一个跨越匹配模式的迭代方法。要设置跨越所有内容的选择对象,可以用一个没有参数的select()方法。

内容的索引和长度总是统计代码单元(code units),而不是代码点(code points)。这类似于传统的ascii字符串处理,然而索引很少在实践中用到选择对象上:子串索引通常与直接操作相反。这就允许对于utf代用品(utf-surrogates)一个更现代化的模型。

字符串支持很大范围的功能,从插入(insert)和移除(removal)到utf编解码(encoding and decoding)。也存在一个不变的子集叫做TextView,旨在简化多线程环境中的生命。然而,TextView必须暴露原状的内容,因此它的不变性在一定程度上依赖于所谓的被调用者名誉( "honour" of a callee)。在这时D不能使不变性强制执行,但该类将被修改以支持这样的特性--通过切片方法(slice())。

这个类是模板化的,可以用到char[]wchar[]dchar[]上,并能无缝迁移编码。特别的,tango.text.Util中的所有函数与任何支持的编码的Text内容相兼容。将来,这个类会变成通向广阔的ICU unicode库的一道主要大门。

注意一些公用的文字操作可以通过结合tango.text.Text tango.text.Util 来完成,比如文字行可以这样处理:

1
2
3
4
auto source = new Text!(char)("one\ntwo\nthree");

foreach (line; Util.lines(source.slice))
         // do something with line

 

说点有点像尤达大师(Yoda)的可以象下面这样完成:

1
2
3
4
auto dst = new Text!(char);

foreach (element; Util.delims ("all cows eat grass", " "))
         dst.prepend (element);

下面是一个API和类层次结构的概述:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class Text(T) : TextView!(T)
{
        // 设置或重设内容
        Text set (T[] chars, bool mutable=true);
        Text set (TextView other, bool mutable=true);

        // 取回当前选择的文字
        T[] selection ();

        // 设置并取回当前选择点
        Text point (uint index);
        uint point ();

        // 标记一个选择对象
        Text select (int start=0, int length=int.max);

        // 返回一个迭代器移动到选择对象周围
        // 也揭示了替换所有(replace all)的功能
        Search search (T chr);
        Search search (T[] pattern);

        // 在当前选择对象后格式化参数
        Text format (T[] format, ...);

        // 在当前选择对象后追加
        Text append (T[] text);
        Text append (TextView other);
        Text append (T chr, int count=1);
        Text append (InputStream source);

        //在当前选择对象后编码( transcodeText encode (char[]);
        Text encode (wchar[]);
        Text encode (dchar[]);

        // 在当前选择对象前插入
        Text prepend (T[] text);
        Text prepend (TextView other);
        Text prepend (T chr, int count=1);

        // 替换当前选择对象
        Text replace (T chr);
        Text replace (T[] text);
        Text replace (TextView other);

        // 移除当前选择对象
        Text remove ();

        // 清除内容
        Text clear ();

        // 修剪行间和结尾空白
        Text trim ();

        // 修剪行间和结尾字符(chr)实例。
        Text strip (T chr);

        //对一个点或当前选择对象截断
        Text truncate (int point = int.max);

        // 为插入/添加保留一些空间
        Text reserve (int extra);

        //写内容到流 
        Text write (OutputStream sink);
}

class TextView(T) : UniText
{
        // 取内容的哈希值
        hash_t toHash ();

        // 返回内容长度
        uint length ();

        // 比较内容
        bool equals  (T[] text);
        bool equals  (TextView other);
        bool ends    (T[] text);
        bool ends    (TextView other);
        bool starts  (T[] text);
        bool starts  (TextView other);
        int compare  (T[] text);
        int compare  (TextView other);
        int opEquals (Object other);
        int opCmp    (Object other);

        // 复制内容
        T[] copy (T[] dst);

        //返回内容
        T[] slice ();

        // 返回数据类型
        typeinfo encoding ();

        // 替换换比较算法(replace the comparison algorithmComparator comparator (Comparator other);
}

class UniText
{
        //转换内容 
        abstract char[]  toString   (char[]  dst = null);
        abstract wchar[] toString16 (wchar[] dst = null);
        abstract dchar[] toString32 (dchar[] dst = null);
}

struct Search
{
        // 选择前一个实例
        bool prev();

        //选择下一个实例 
        bool next();

        // 返回实例计数
        size_t count();

        // 是否包含实例?
        bool within();

        // 用一个字符替换所有(字符)
        void replace(T);

        //用文本替换所有(字符) (null == remove all)
        void replace(T[]);
}
void memmove(void* dst, void* src, uint bytes) [private, extern(C)] ¶# class Text(T) : TextView!(T) ¶#
该可变 Text(文本)类实际上实现了完整的 API,而超类纯粹是一种抽象 (可以用接口代替 )
struct Search(T) ¶#
搜索迭代器
Search opCall(Text text, T[] match) [static] ¶#
构造一个 Search实例
bool prev() ¶#
向后搜索,开始于选择点之前的字符。
bool next() ¶#
向后搜索,从当前选定的文字后开始。
bool within() ¶#
如果与关联的文字匹配就返回 true
size_t count() ¶#
返回与关联文字匹配的数字。
void replace(T chr) ¶#
替换所有与给定字符匹配的字符。
void replace(T[] sub = null) ¶#
替换所有与给定子串匹配的字符串。
bool locate(Call call, T[] content, size_t from) [private] ¶#
视情况查找索引和选择的模式。 locate pattern index and select as appropriate
struct Span [public, deprecated] ¶# 选择跨度 Selection span

废弃:

point()代替
uint begin ¶# uint length ¶#
选择对象的长度。
this(uint space = 0) ¶# 使用指定可用空间创建一个空的 Text

注:

一个像'a'这样的字符会被隐式转换成uint,从而让这个构造器接受,使它看起来可以用一个单一的字符来初始化一个Text实例,省略了不支持的某些东西(something which is not supported)。

this(T[] content, bool copy = true) ¶#
用提供的内容创建一个 Text。如果说内容是不变的(只读)你可以考虑设置 'copy'参数为 false。这样做可以避免为内容分本堆空间直到它通过 Text方法被修改。当包装一个基于栈的 Text临时数组时这样做非常有用。
this(TextViewT other, bool copy = true) ¶#
用另一个的内容创建一个 Text。如果说内容是不变的(只读)你可以考虑设置 'copy'参数为 false。这样做可以避免为内容分本堆空间直到它通过 Text方法被修改。当包装一个基于栈的 Text临时数组时这样做非常有用。
Text set(T[] chars, bool copy = true) [final] ¶#
设置内容到提供的数组。 'copy'参数指明给定的数组是否可能改变。如果不能,该数组被别名,直到它可以通过这个类改变的时间。这样做可以避免为内容分本堆空间直到它通过 Text方法被修改。当包装一个基于栈的 Text临时数组时这样做非常有用。
也可以重设当前选择对象为空( null)。
Text set(TextViewT other, bool copy = true) [final] ¶#
替换这个 Text的内容。如果新内容是不变的(只读)你可以考虑设置 'copy'参数为 false。这样做可以避免为内容分本堆空间直到它通过 Text方法被修改。当包装一个基于栈的 Text临时数组时这样做非常有用。
也可以重设当前选择对象为空( null)。
Text select(int start = 0, int length = int.max) [final] ¶#
显式设置当前选择对象到给定开始和长度。值被拴( pinned)到内容范围。
T[] selection() [final] ¶#
返回当前选择的内容。
Span span() [final, deprecated] ¶#
返回当前选择对象的索引和长度。
废弃:
使用 point()代替。

uint point() [final] ¶#
返回当前选择点。
Text point(uint index) [final] ¶#
设置当前选择点,并重设选择对象长度。
Search!(T) search(T[] match) ¶#
为给定模式返回一个搜索迭代器。视情况迭代器设置当前文字选择对象。如:
1
2
3
4
5
auto t = new Text ("hello world");
auto s = t.search ("world");

assert (s.next);
assert (t.selection == "world");
使用类似时尚的方式进行替换模式操作:
1
2
3
4
5
6
auto t = new Text ("hello world");
auto s = t.search ("world");

// 替换所有"world"实例为"everyone"
assert (s.replace ("everyone"));
assert (s.count is 0);
bool select(T c) [final, deprecated] ¶#
查找和选择一个字符串中 BMP编码点的下一个出现。如果找到返回 true,否则将返回 false

废弃:

search() 代替。
bool select(TextViewT other) [final, deprecated] ¶#
查找和选择下一个子串的出现。如果找到返回 true,否则将返回 false

废弃:

search() 代替。
bool select(T[] chars) [final, deprecated] ¶#
查找和选择下一个子串的出现。如果找到返回 true,否则将返回 false

废弃:

search() 代替。
bool selectPrior(T c) [final, deprecated] ¶#
查找和选择一个字符串中 BMP编码点的前一个出现。如果找到返回 true,否则将返回 false

废弃:

search() 代替。
bool selectPrior(TextViewT other
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值