File: text.cpp

package info (click to toggle)
obby 0.4.6-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,800 kB
  • ctags: 1,512
  • sloc: cpp: 11,181; sh: 9,627; makefile: 235; perl: 29; sed: 16
file content (988 lines) | stat: -rw-r--r-- 22,047 bytes parent folder | download | duplicates (4)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/* libobby - Network text editing library
 * Copyright (C) 2005, 2006 0x539 dev group
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "text.hpp"

namespace
{
	const obby::text::size_type CHUNK_INIT =
		~static_cast<obby::text::size_type>(0);

	inline obby::text::size_type CHUNK_SIZE(obby::text::size_type size)
	{
		return size == obby::text::npos ? CHUNK_INIT : size;
	}

	// find_chunk implementation to avoid code duplication for
	// iterator and const_iterator versions
	template<typename List, typename Iter>
	Iter find_chunk(List list, obby::text::size_type& pos)
	{
		for(Iter it = list.begin(); it != list.end(); ++ it)
		{
			if( (*it)->get_length() > pos)
				return it;
			else
				pos -= (*it)->get_length();
		}

		if(pos == 0) return list.end();

		throw std::logic_error(
			"obby::text::find_chunk:\n"
			"Requested position exceeds text's size"
		);
	}
}

obby::text::chunk::chunk(const chunk& other):
	m_text(other.m_text), m_author(other.m_author)
{
}

obby::text::chunk::chunk(const string_type& string,
                         const user* author):
	m_text(string),
	m_author(author)
{
}

obby::text::chunk::chunk(const net6::packet& pack,
                         unsigned int& index,
                         const user_table& table):
	m_text(pack.get_param(index + 0).as<std::string>() ),
	m_author(
		pack.get_param(index + 1).as<const user*>(
			::serialise::hex_context_from<const user*>(table)
		)
	)
{
	index += 2;
}

obby::text::chunk::chunk(const serialise::object& obj,
                         const user_table& table):
	m_text(obj.get_required_attribute("content").as<std::string>() ),
	m_author(
		obj.get_required_attribute("author").as<const user*>(
			::serialise::default_context_from<const user*>(table)
		)
	)
{
}

void obby::text::chunk::serialise(serialise::object& obj) const
{
	obj.add_attribute("content").set_value(m_text);
	obj.add_attribute("author").set_value(m_author);
}

void obby::text::chunk::append_packet(net6::packet& pack) const
{
	pack << m_text << m_author;
}

void obby::text::chunk::prepend(const string_type& text)
{
	m_text.insert(0, text);
}

void obby::text::chunk::append(const string_type& text)
{
	m_text.append(text);
}

void obby::text::chunk::insert(size_type pos, const string_type& text)
{
	m_text.insert(pos, text);
}

void obby::text::chunk::erase(size_type pos, size_type len)
{
	m_text.erase(pos, len);
}

const obby::text::string_type& obby::text::chunk::get_text() const
{
	return m_text;
}

const obby::user* obby::text::chunk::get_author() const
{
	return m_author;
}

obby::text::size_type obby::text::chunk::get_length() const
{
	return m_text.length();
}

obby::text::text(size_type initial_chunk_size):
	m_max_chunk(CHUNK_SIZE(initial_chunk_size) )
{
}

obby::text::text(const text& other):
	m_max_chunk(other.m_max_chunk)
{
	for(list_type::const_iterator iter = other.m_chunks.begin();
	    iter != other.m_chunks.end();
	    ++ iter)
	{
		m_chunks.push_back(new chunk(**iter) );
	}
}

obby::text::text(const string_type& string,
                 const user* author,
                 size_type initial_chunk_size):
	m_max_chunk(CHUNK_SIZE(initial_chunk_size) )
{
	for(size_type n = 0; n < string.length(); ++ n)
	{
		size_type len = std::min(string.length() - n, m_max_chunk);
		m_chunks.push_back(new chunk(string.substr(n, len), author) );
	}
}

obby::text::text(const net6::packet& pack,
                 unsigned int& index,
                 const user_table& table):
	m_max_chunk(CHUNK_INIT)
{
	unsigned int count = pack.get_param(index ++).as<unsigned int>();
	for(unsigned int i = 0; i < count; ++ i)
		m_chunks.push_back(new chunk(pack, index, table) );
}

obby::text::text(const serialise::object& obj,
                 const user_table& table):
	m_max_chunk(CHUNK_INIT)
{
	for(serialise::object::child_iterator iter = obj.children_begin();
	    iter != obj.children_end();
	    ++ iter)
	{
		if(iter->get_name() == "chunk")
		{
			m_chunks.push_back(new chunk(*iter, table) );
		}
		else
		{
			// TODO: unexpected_child_error
			format_string str(_("Unexpected child node: '%0%'") );
			str << iter->get_name();
			throw serialise::error(str.str(), iter->get_line() );
		}
	}
}

obby::text::~text()
{
	clear();
}

obby::text& obby::text::operator=(const text& other)
{
	if(&other == this) return *this;

	clear();
	m_max_chunk = other.m_max_chunk;

	for(list_type::const_iterator iter = other.m_chunks.begin();
	    iter != other.m_chunks.end();
	    ++ iter)
	{
		m_chunks.push_back(new chunk(**iter) );
	}

	return *this;
}

void obby::text::serialise(serialise::object& obj) const
{
	for(list_type::const_iterator it = m_chunks.begin();
	    it != m_chunks.end();
	    ++ it)
	{
		serialise::object& part = obj.add_child();
		part.set_name("chunk");
		(*it)->serialise(part);
	}
}

void obby::text::append_packet(net6::packet& pack) const
{
	pack << m_chunks.size();
	for(list_type::const_iterator it = m_chunks.begin();
	    it != m_chunks.end();
	    ++ it)
	{
		(*it)->append_packet(pack);
	}
}

void obby::text::clear()
{
	for(list_type::iterator it = m_chunks.begin();
	    it != m_chunks.end();
	    ++ it)
	{
		delete *it;
	}

	m_chunks.clear();
}

obby::text obby::text::substr(size_type pos, size_type len) const
{
	text new_text;
	list_type::const_iterator iter = find_chunk(pos);

	chunk* prev_chunk = NULL;
	while( (len == npos || len > 0) && (iter != m_chunks.end()) )
	{
		chunk* cur_chunk = *iter;
		size_type count = cur_chunk->get_length() - pos;

		if(len != npos)
		{
			count = std::min(count, len);
			len -= count;
		}

		if(prev_chunk != NULL &&
		   prev_chunk->get_author() == cur_chunk->get_author() &&
		   prev_chunk->get_length() + cur_chunk->get_length() <=
		   m_max_chunk)
		{
			prev_chunk->append(
				cur_chunk->get_text().substr(pos, count)
			);
		}
		else
		{
			prev_chunk = new chunk(
				cur_chunk->get_text().substr(pos, count),
				cur_chunk->get_author()
			);

			new_text.m_chunks.push_back(prev_chunk);
		}

		++ iter; pos = 0;
	}

	if(len > 0 && len != npos)
	{
		throw std::logic_error(
			"obby::text::substr:\n"
			"len is out or range"
		);
	}

	return new_text;
}

void obby::text::insert(size_type pos,
                        const string_type& str,
                        const user* author)
{
	list_type::iterator ins_pos = find_chunk(pos);
	insert_chunk(ins_pos, pos, str, author);
}

void obby::text::insert(size_type pos,
                        const text& str)
{
	list_type::iterator ins_pos = find_chunk(pos);
	for(list_type::const_iterator it = str.m_chunks.begin();
	    it != str.m_chunks.end();
	    ++ it)
	{
		ins_pos = insert_chunk(
			ins_pos,
			pos,
			(*it)->get_text(),
			(*it)->get_author()
		);
	}
}

void obby::text::erase(size_type pos, size_type len)
{
	list_type::iterator ers_pos = find_chunk(pos);
	list_type::iterator first_chunk = ers_pos;
	size_type first_pos = pos;

	// Remember first chunk position that will not be removed.
	// After having erased a chunk in the middle, something may have
	// been mergen with this first chunk that should have been deleted.
	// We watch it and remove merged stuff if the chunk grew.
	if(first_pos == 0 && first_chunk != m_chunks.begin() )
	{
		-- first_chunk;
		first_pos = (*first_chunk)->get_length();
	}

	while( (len == npos || len > 0) && ers_pos != m_chunks.end() )
	{
		size_type count = (*ers_pos)->get_length() - pos;
		if(len != npos)
		{
			count = std::min(count, len);
			len -= count;
		}

		ers_pos = erase_chunk(ers_pos, pos, count);

		if(first_pos > 0 && (*first_chunk)->get_length() > first_pos)
		{
			ers_pos = first_chunk;
			pos = first_pos;
		}
		else
		{
			pos = 0;
		}
	}

	if(len != npos && len > 0)
	{
		throw std::logic_error(
			"obby::text::erase:\n"
			"len is out of range"
		);
	}
}

void obby::text::append(const string_type& str,
                        const user* author)
{
	chunk* last_chunk = NULL;
	if(!m_chunks.empty() ) last_chunk = *m_chunks.rbegin();
	size_type pos = 0;

	// Merge beginning of str with last chunk if possible
	if(last_chunk != NULL &&
	   last_chunk->get_author() == author &&
	   last_chunk->get_length() < m_max_chunk)
	{
		pos = std::min(
			m_max_chunk - last_chunk->get_length(),
			str.length()
		);

		last_chunk->append(str.substr(0, pos) );
	}

	// Append rest of string
	for(; pos < str.length(); pos += m_max_chunk)
	{
		size_type count = std::min(str.length() - pos, m_max_chunk);
		m_chunks.push_back(new chunk(str.substr(pos, count), author) );
	}
}

void obby::text::append(const text& str)
{
	// Simply append all chunks of str
	for(list_type::const_iterator it = str.m_chunks.begin();
	    it != str.m_chunks.end();
	    ++ it)
	{
		append( (*it)->get_text(), (*it)->get_author() );
	}
}

void obby::text::prepend(const string_type& str,
                         const user* author)
{
	chunk* first_chunk = NULL;
	if(!m_chunks.empty() ) first_chunk = *m_chunks.begin();

	size_type len = str.length();

	// Prepend end of str to first chunk if possible
	if(first_chunk != NULL &&
	   first_chunk->get_author() == author &&
	   first_chunk->get_length() < m_max_chunk)
	{
		size_type count = std::min(
			m_max_chunk - first_chunk->get_length(),
			len
		);

		len -= count;
		first_chunk->prepend(str.substr(len, count) );
	}

	// Insert chunks before for the rest of str
	while(len > 0)
	{
		size_type count = std::min(
			len,
			m_max_chunk
		);

		len -= count;
		m_chunks.push_front(new chunk(str.substr(len, count), author));
	}
}

void obby::text::prepend(const text& str)
{
	for(list_type::const_reverse_iterator it = str.m_chunks.rbegin();
	    it != str.m_chunks.rend();
	    ++ it)
	{
		prepend( (*it)->get_text(), (*it)->get_author() );
	}
}

obby::text::size_type obby::text::length() const
{
	// TODO: Cache this value?
	size_type len = 0;
	for(list_type::const_iterator it = m_chunks.begin();
	    it != m_chunks.end();
	    ++ it)
	{
		len += (*it)->get_length();
	}

	return len;
}

bool obby::text::operator==(const text& other) const
{
	return compare(other) == EQUAL_OWNERMATCH;
}

bool obby::text::operator!=(const text& other) const
{
	return compare(other) != EQUAL_OWNERMATCH;
}

bool obby::text::operator<(const text& other) const
{
	return compare(other) == LESS;
}

bool obby::text::operator>(const text& other) const
{
	return compare(other) == GREATER;
}

bool obby::text::operator<=(const text& other) const
{
	return compare(other) != GREATER;
}

bool obby::text::operator>=(const text& other) const
{
	return compare(other) != LESS;
}

bool obby::text::operator==(const string_type& other) const
{
	return compare(other) == EQUAL;
}

bool obby::text::operator!=(const string_type& other) const
{
	return compare(other) != EQUAL;
}

bool obby::text::operator<(const string_type& other) const
{
	return compare(other) == LESS;
}

bool obby::text::operator>(const string_type& other) const
{
	return compare(other) == GREATER;
}

bool obby::text::operator<=(const string_type& other) const
{
	return compare(other) != GREATER;
}

bool obby::text::operator>=(const string_type& other) const
{
	return compare(other) != LESS;
}

bool obby::text::empty() const
{
	return m_chunks.empty();
}

obby::text::chunk_iterator obby::text::chunk_begin() const
{
	return chunk_iterator(m_chunks.begin() );
}

obby::text::chunk_iterator obby::text::chunk_end() const
{
	return chunk_iterator(m_chunks.end() );
}

void obby::text::set_max_chunk_size(size_type max_chunk)
{
	m_max_chunk = max_chunk;
	if(m_chunks.empty() ) return;

	// Merge and/or split current chunks
	list_type::iterator next = m_chunks.begin(); ++ next;
	for(list_type::iterator it = m_chunks.begin();
	    it != m_chunks.end();
	    ++ it, ++ next)
	{
		chunk* cur_chunk = *it;

		chunk* next_chunk = NULL;
		if(next != m_chunks.end() ) next_chunk = *next;

		// Split current chunk if necessary
		if(cur_chunk->get_length() > m_max_chunk)
		{
			size_type pos = m_max_chunk;
			while(cur_chunk->get_length() - pos > 0)
			{
				// Merge with next if possible
				if(next_chunk != NULL &&
				   next_chunk->get_author() ==
				   cur_chunk->get_author() &&
				   cur_chunk->get_length() - pos +
				   next_chunk->get_length() <= m_max_chunk)
				{
					// Note that this could also be done
					// in the merging process below but
					// then we would split the chunk up
					// just to merge it then...
					next_chunk->prepend(
						cur_chunk->get_text().substr(
							pos
						)
					);

					pos += (cur_chunk->get_length() - pos);
				}
				// Split otherwise
				else
				{
					size_type len = std::min(
						cur_chunk->get_length() - pos,
						m_max_chunk
					);

					const std::string& text =
						cur_chunk->get_text();

					it = m_chunks.insert(
						next,
						new chunk(
							text.substr(
								pos,
								len
							),
							cur_chunk->get_author()
						)
					);

					pos += len;
				}

			}

			// Remove splitted/merged stuff from current one
			cur_chunk->erase(m_max_chunk);
			cur_chunk = *it;
		}
		// Merge chunk with next
		else if(next_chunk != NULL &&
		        cur_chunk->get_author() == next_chunk->get_author() &&
		        cur_chunk->get_length() + next_chunk->get_length() <=
		        m_max_chunk)
		{
			cur_chunk->append(next_chunk->get_text() );

			delete next_chunk;
			next = m_chunks.erase(next);
		}
	}
}

obby::text::operator string_type() const
{
	string_type str;
	str.reserve(length() );

	for(list_type::const_iterator it = m_chunks.begin();
	    it != m_chunks.end();
	    ++ it)
	{
		str.append( (*it)->get_text() );
	}

	return str;
}

obby::text::list_type::iterator
obby::text::find_chunk(size_type& pos)
{
	return ::find_chunk<list_type&, list_type::iterator>(
		m_chunks,
		pos
	);
}

obby::text::list_type::const_iterator
obby::text::find_chunk(size_type& pos) const
{
	return ::find_chunk<const list_type&, list_type::const_iterator>(
		m_chunks,
		pos
	);
}

obby::text::list_type::iterator
obby::text::insert_chunk(list_type::iterator chunk_it,
                         size_type& chunk_pos,
                         const string_type& str,
                         const user* author)
{
	chunk* cur_chunk = NULL;
	if(chunk_it != m_chunks.end() ) cur_chunk = *chunk_it;

	list_type::iterator ins_pos = chunk_it;

	// Get previous chunk
	chunk* prev_chunk = NULL;
	list_type::iterator prev_pos = ins_pos;
	if(prev_pos != m_chunks.begin() )
	{
		-- prev_pos;
		prev_chunk = *prev_pos;
	}

	// Merge with previous
	if(prev_chunk != NULL &&
	   chunk_pos == 0 &&
	   author == prev_chunk->get_author() &&
	   str.length() + prev_chunk->get_length() <= m_max_chunk)
	{
		prev_chunk->append(str);
		return chunk_it;
	}
	else if(cur_chunk == NULL)
	{
		// Insertion at end (no current chunk) and cannot merge with
		// previous: Need to create a new one, do nothing here - this
		// is done below
	}
	// Merge with current
	else if(author == cur_chunk->get_author() &&
	        str.length() + cur_chunk->get_length() <= m_max_chunk)
	{
		cur_chunk->insert(chunk_pos, str);
		chunk_pos += str.length();
		return chunk_it;
	}
	// Insert new chunk after current chunk if str is inserted at
	// end of current chunk
	else if(chunk_pos == cur_chunk->get_length() )
	{
		++ ins_pos;
	}
	// Insert new chunk before current chunk if str is inserted at
	// the beginning of current chunk
	else if(chunk_pos > 0)
	{
		// Split up otherwise
		chunk* new_chunk = new chunk(
			cur_chunk->get_text().substr(chunk_pos),
			cur_chunk->get_author()
		);

		cur_chunk->erase(chunk_pos);
		chunk_pos = 0;

		++ ins_pos;
		ins_pos = m_chunks.insert(
			ins_pos,
			new_chunk
		);

		// Try to merge with both chunks - they may be smaller
		// and thus fit into max chunk size
		if(cur_chunk->get_author() == author)
		{
			if(cur_chunk->get_length() + str.length() <=
			   m_max_chunk)
			{
				cur_chunk->append(str);
				chunk_pos = cur_chunk->get_length();
				-- ins_pos;
				return ins_pos;
			}
			else if(new_chunk->get_length() +
			        str.length() <= m_max_chunk)
			{
				new_chunk->prepend(str);
				chunk_pos = str.length();
				return ins_pos;
			}
		}

		// If not insert another new chunk between
		// the chunks split up - ins_pos points already
		// to the correct position
	}

	// Insert one new chunk if str fits into
	if(str.length() <= m_max_chunk)
	{
		chunk_pos = 0;
		m_chunks.insert(ins_pos, new chunk(str, author) );
		return ins_pos;
	}
	else
	{
		// Make multiple chunks otherwise
		// TODO: Fill up previous chunk if author matches and ins_pos
		// is != m_chunks.begin()
		cur_chunk = ( (ins_pos == m_chunks.end()) ? NULL : *ins_pos);
		for(size_type n = 0; n < str.length(); n += m_max_chunk)
		{
			size_type len = std::min(str.length() - n, m_max_chunk);

			// Merge with next
			if(cur_chunk &&
			   cur_chunk->get_author() == author &&
			   len + cur_chunk->get_length() <= m_max_chunk)
			{
				// Must be last chunk to insert since all
				// others are m_max_chunk in size and thus
				// may not be merged
				cur_chunk->prepend(str.substr(n, len) );
				chunk_pos = len;
				return ins_pos;
			}
			else
			{
				/*result =*/ m_chunks.insert(
					ins_pos,
					new chunk(str.substr(n, len), author)
				);
			}
		}

		chunk_pos = 0;
		return ins_pos;
	}
}

obby::text::list_type::iterator
obby::text::erase_chunk(list_type::iterator chunk_it,
                        size_type pos,
                        size_type len)
{
	chunk* prev_chunk = NULL;
	chunk* next_chunk = NULL;

	list_type::iterator prev_it = chunk_it;
	if(prev_it != m_chunks.begin() ) { -- prev_it; prev_chunk = *prev_it; }

	list_type::iterator next_it = chunk_it;
	++ next_it;
	if(next_it != m_chunks.end() ) { next_chunk = *next_it; }

	chunk* cur_chunk = *chunk_it;
	//if(len == npos) len = cur_chunk->get_length() - pos;

	if(pos + len > cur_chunk->get_length() )
	{
		throw std::logic_error(
			"obby::text::erase_chunk:\n"
			"Chunk len exceeded"
		);
	}

	// Complete erasure
	if(len == cur_chunk->get_length() )
	{
		delete cur_chunk;
		m_chunks.erase(chunk_it);

		// Merge surrounding chunks if possible
		if(next_chunk != NULL && prev_chunk != NULL &&
		   next_chunk->get_author() == prev_chunk->get_author() &&
		   next_chunk->get_length() + prev_chunk->get_length() <
		   m_max_chunk)
		{
			prev_chunk->append(next_chunk->get_text() );

			delete next_chunk;
			next_it = m_chunks.erase(next_it);
		}

		return next_it;
	}

	// Merge with previous
	if(prev_chunk != NULL &&
	   prev_chunk->get_author() == cur_chunk->get_author() &&
	   cur_chunk->get_length() - len + prev_chunk->get_length() <
	   m_max_chunk)
	{
		if(pos > 0)
		{
			prev_chunk->append(
				cur_chunk->get_text().substr(0, pos)
			);
		}

		if(pos + len < cur_chunk->get_length() )
		{
			prev_chunk->append(
				cur_chunk->get_text().substr(pos + len)
			);
		}

		delete cur_chunk;
		m_chunks.erase(chunk_it);

		// Merge result with next if possible
		if(next_chunk != NULL &&
		   prev_chunk->get_author() == next_chunk->get_author() &&
		   prev_chunk->get_length() + next_chunk->get_length() <=
		   m_max_chunk)
		{
			prev_chunk->append(next_chunk->get_text() );

			delete next_chunk;
			next_it = m_chunks.erase(next_it);
		}

		return next_it;
	}

	// Merge with next
	if(next_chunk != NULL &&
	   next_chunk->get_author() == cur_chunk->get_author() &&
	   cur_chunk->get_length() - len + next_chunk->get_length() <
	   m_max_chunk)
	{
		if(pos + len < cur_chunk->get_length() )
		{
			next_chunk->prepend(
				cur_chunk->get_text().substr(pos)
			);
		}

		if(pos > 0)
		{
			next_chunk->prepend(
				cur_chunk->get_text().substr(0, pos)
			);
		}

		delete cur_chunk;
		m_chunks.erase(chunk_it);

		// No need to try to merge with previous since the check
		// above would already have done it.

		++ next_it;
		return next_it;
	}

	// No merging possible...
	cur_chunk->erase(pos, len);
	return next_it;
}

obby::text::compare_result obby::text::compare(const text& other) const
{
	list_type::const_iterator it1 = m_chunks.begin();
	list_type::const_iterator it2 = other.m_chunks.begin();

	size_type pos1 = 0, pos2 = 0;
	bool author_match = true;

	while(it1 != m_chunks.end() && it2 != other.m_chunks.end() )
	{
		// Authors do not match: Remember this for later use. If
		// the strings differ, LESS or GREATER is returned; author
		// match does only play a role when the text content is equal
		if( (*it1)->get_author() != (*it2)->get_author() )
			author_match = false;

		size_type len = std::min(
			(*it1)->get_length() - pos1,
			(*it2)->get_length() - pos2
		);

		int res = (*it1)->get_text().compare(
			pos1,
			len,
			(*it2)->get_text(),
			pos2,
			len
		);

		if(res != 0) return res < 0 ? LESS : GREATER;

		pos1 += len;
		pos2 += len;

		if(pos1 == (*it1)->get_length() )
			{ ++ it1; pos1 = 0; }
		if(pos2 == (*it2)->get_length() )
			{ ++ it2; pos2 = 0; }
	}

	// *this has more length than other
	if(it1 != m_chunks.end() )
		return GREATER;
	// other has more length
	else if(it2 != other.m_chunks.end() )
		return LESS;
	// both text's content are equal
	else
		return author_match ? EQUAL_OWNERMATCH : EQUAL;
}

obby::text::compare_result obby::text::compare(const string_type& text) const
{
	size_type pos = 0;
	for(list_type::const_iterator it = m_chunks.begin();
	    it != m_chunks.end();
	    ++ it)
	{
		size_type len = (*it)->get_length();
		int res = text.compare(pos, len, (*it)->get_text());
		if(res != 0) return res < 0 ? LESS : GREATER;
		pos += len;
	}

	return EQUAL;
}