redcarpet 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of redcarpet might be problematic. Click here for more details.
- data/COPYING +14 -0
- data/README.markdown +38 -0
- data/Rakefile +135 -0
- data/bin/redcarpet +13 -0
- data/ext/array.c +300 -0
- data/ext/array.h +148 -0
- data/ext/buffer.c +318 -0
- data/ext/buffer.h +147 -0
- data/ext/extconf.rb +4 -0
- data/ext/markdown.c +1590 -0
- data/ext/markdown.h +124 -0
- data/ext/redcarpet.c +86 -0
- data/ext/render.c +499 -0
- data/lib/markdown.rb +1 -0
- data/lib/redcarpet.rb +72 -0
- data/redcarpet.gemspec +39 -0
- data/test/benchmark.rb +56 -0
- data/test/benchmark.txt +306 -0
- data/test/markdown_test.rb +176 -0
- data/test/redcarpet_test.rb +106 -0
- metadata +88 -0
data/COPYING
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Copyright (c) 2009, Natacha Porté
|
2
|
+
Copyright (c) 2011, Vicent Marti
|
3
|
+
|
4
|
+
Permission to use, copy, modify, and distribute this software for any
|
5
|
+
purpose with or without fee is hereby granted, provided that the above
|
6
|
+
copyright notice and this permission notice appear in all copies.
|
7
|
+
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
Markdown + Ruby + libupskirt
|
2
|
+
============================
|
3
|
+
|
4
|
+
> Inspired by Rick Astley wearing a kilt
|
5
|
+
|
6
|
+
Upskirt is an implementation of John Gruber's Markdown markup
|
7
|
+
language. Upskirt is safe, fast and production ready. Check out
|
8
|
+
the original version at <http://git.instinctive.eu/cgit/libupskirt/>
|
9
|
+
|
10
|
+
Redcarpet is Upskirt with a touch of Ruby. It is mostly based on Ryan
|
11
|
+
Tomayko's RDiscount wrapper, and inspired by Rick Astley wearing a kilt.
|
12
|
+
|
13
|
+
Redcarpet is powered by a modified version of Upskirt, which has been
|
14
|
+
updated to pass the official Markdown test suite and now has support
|
15
|
+
for many additional features: autolinks, smartypants, safe filters,
|
16
|
+
and a long etcetera.
|
17
|
+
|
18
|
+
Redcarpet is a drop-in replacement for BlueCloth, RedCloth and RDiscount.
|
19
|
+
|
20
|
+
* Upskirt is (C)2009 Natacha Porté
|
21
|
+
* Upskirt has been brought back to life and made standards-compilant in 2011 by Vicent Marti
|
22
|
+
* Redcarpet is (C)2011 Vicent Marti
|
23
|
+
|
24
|
+
|
25
|
+
License
|
26
|
+
-------
|
27
|
+
|
28
|
+
Permission to use, copy, modify, and distribute this software for any
|
29
|
+
purpose with or without fee is hereby granted, provided that the above
|
30
|
+
copyright notice and this permission notice appear in all copies.
|
31
|
+
|
32
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
33
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
34
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
35
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
36
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
37
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
38
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'rake/clean'
|
3
|
+
require 'digest/md5'
|
4
|
+
|
5
|
+
task :default => :test
|
6
|
+
|
7
|
+
# ==========================================================
|
8
|
+
# Ruby Extension
|
9
|
+
# ==========================================================
|
10
|
+
|
11
|
+
DLEXT = Config::MAKEFILE_CONFIG['DLEXT']
|
12
|
+
RUBYDIGEST = Digest::MD5.hexdigest(`#{RUBY} --version`)
|
13
|
+
|
14
|
+
file "ext/ruby-#{RUBYDIGEST}" do |f|
|
15
|
+
rm_f FileList["ext/ruby-*"]
|
16
|
+
touch f.name
|
17
|
+
end
|
18
|
+
CLEAN.include "ext/ruby-*"
|
19
|
+
|
20
|
+
file 'ext/Makefile' => FileList['ext/*.{c,h,rb}', "ext/ruby-#{RUBYDIGEST}"] do
|
21
|
+
chdir('ext') { ruby 'extconf.rb' }
|
22
|
+
end
|
23
|
+
CLEAN.include 'ext/Makefile', 'ext/mkmf.log'
|
24
|
+
|
25
|
+
file "ext/redcarpet.#{DLEXT}" => FileList["ext/Makefile"] do |f|
|
26
|
+
sh 'cd ext && make clean && make && rm -rf conftest.dSYM'
|
27
|
+
end
|
28
|
+
CLEAN.include 'ext/*.{o,bundle,so,dll}'
|
29
|
+
|
30
|
+
file "lib/redcarpet.#{DLEXT}" => "ext/redcarpet.#{DLEXT}" do |f|
|
31
|
+
cp f.prerequisites, "lib/", :preserve => true
|
32
|
+
end
|
33
|
+
|
34
|
+
desc 'Build the redcarpet extension'
|
35
|
+
task :build => "lib/redcarpet.#{DLEXT}"
|
36
|
+
|
37
|
+
# ==========================================================
|
38
|
+
# Testing
|
39
|
+
# ==========================================================
|
40
|
+
|
41
|
+
require 'rake/testtask'
|
42
|
+
Rake::TestTask.new('test:unit') do |t|
|
43
|
+
t.test_files = FileList['test/*_test.rb']
|
44
|
+
t.ruby_opts += ['-rubygems'] if defined? Gem
|
45
|
+
end
|
46
|
+
task 'test:unit' => [:build]
|
47
|
+
|
48
|
+
desc 'Run conformance tests (MARKDOWN_TEST_VER=1.0)'
|
49
|
+
task 'test:conformance' => [:build] do |t|
|
50
|
+
script = "#{pwd}/bin/redcarpet"
|
51
|
+
test_version = ENV['MARKDOWN_TEST_VER'] || '1.0.3'
|
52
|
+
lib_dir = "#{pwd}/lib"
|
53
|
+
chdir("test/MarkdownTest_#{test_version}") do
|
54
|
+
sh "RUBYLIB=#{lib_dir} ./MarkdownTest.pl --script='#{script}' --tidy"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
desc 'Run version 1.0 conformance suite'
|
59
|
+
task 'test:conformance:1.0' => [:build] do |t|
|
60
|
+
ENV['MARKDOWN_TEST_VER'] = '1.0'
|
61
|
+
Rake::Task['test:conformance'].invoke
|
62
|
+
end
|
63
|
+
|
64
|
+
desc 'Run 1.0.3 conformance suite'
|
65
|
+
task 'test:conformance:1.0.3' => [:build] do |t|
|
66
|
+
ENV['MARKDOWN_TEST_VER'] = '1.0.3'
|
67
|
+
Rake::Task['test:conformance'].invoke
|
68
|
+
end
|
69
|
+
|
70
|
+
desc 'Run unit and conformance tests'
|
71
|
+
task :test => %w[test:unit test:conformance]
|
72
|
+
|
73
|
+
desc 'Run benchmarks'
|
74
|
+
task :benchmark => :build do |t|
|
75
|
+
$:.unshift 'lib'
|
76
|
+
load 'test/benchmark.rb'
|
77
|
+
end
|
78
|
+
|
79
|
+
# PACKAGING =================================================================
|
80
|
+
|
81
|
+
require 'rubygems'
|
82
|
+
$spec = eval(File.read('redcarpet.gemspec'))
|
83
|
+
|
84
|
+
def package(ext='')
|
85
|
+
"pkg/redcarpet-#{$spec.version}" + ext
|
86
|
+
end
|
87
|
+
|
88
|
+
desc 'Build packages'
|
89
|
+
task :package => %w[.gem .tar.gz].map {|e| package(e)}
|
90
|
+
|
91
|
+
desc 'Build and install as local gem'
|
92
|
+
task :install => package('.gem') do
|
93
|
+
sh "gem install #{package('.gem')}"
|
94
|
+
end
|
95
|
+
|
96
|
+
desc 'Update the gemspec'
|
97
|
+
task :update_gem => file('redcarpet.gemspec')
|
98
|
+
|
99
|
+
directory 'pkg/'
|
100
|
+
|
101
|
+
file package('.gem') => %w[pkg/ redcarpet.gemspec] + $spec.files do |f|
|
102
|
+
sh "gem build redcarpet.gemspec"
|
103
|
+
mv File.basename(f.name), f.name
|
104
|
+
end
|
105
|
+
|
106
|
+
file package('.tar.gz') => %w[pkg/] + $spec.files do |f|
|
107
|
+
sh "git archive --format=tar HEAD | gzip > #{f.name}"
|
108
|
+
end
|
109
|
+
|
110
|
+
# GEMSPEC HELPERS ==========================================================
|
111
|
+
|
112
|
+
def source_version
|
113
|
+
line = File.read('lib/redcarpet.rb')[/^\s*VERSION = .*/]
|
114
|
+
line.match(/.*VERSION = '(.*)'/)[1]
|
115
|
+
end
|
116
|
+
|
117
|
+
file 'redcarpet.gemspec' => FileList['Rakefile','lib/redcarpet.rb'] do |f|
|
118
|
+
# read spec file and split out manifest section
|
119
|
+
spec = File.read(f.name)
|
120
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
121
|
+
head.sub!(/\.version = '.*'/, ".version = '#{source_version}'")
|
122
|
+
head.sub!(/\.date = '.*'/, ".date = '#{Date.today.to_s}'")
|
123
|
+
# determine file list from git ls-files
|
124
|
+
files = `git ls-files`.
|
125
|
+
split("\n").
|
126
|
+
sort.
|
127
|
+
reject{ |file| file =~ /^\./ || file =~ /^test\/MarkdownTest/ }.
|
128
|
+
map{ |file| " #{file}" }.
|
129
|
+
join("\n")
|
130
|
+
# piece file back together and write...
|
131
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
132
|
+
spec = [head,manifest,tail].join(" # = MANIFEST =\n")
|
133
|
+
File.open(f.name, 'w') { |io| io.write(spec) }
|
134
|
+
puts "updated #{f.name}"
|
135
|
+
end
|
data/bin/redcarpet
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Usage: redcarpet [<file>...]
|
3
|
+
# Convert one or more Markdown files to HTML and write to standard output. With
|
4
|
+
# no <file> or when <file> is '-', read Markdown source text from standard input.
|
5
|
+
if ARGV.include?('--help')
|
6
|
+
File.read(__FILE__).split("\n").grep(/^# /).each do |line|
|
7
|
+
puts line[2..-1]
|
8
|
+
end
|
9
|
+
exit 0
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'redcarpet'
|
13
|
+
STDOUT.write(Redcarpet.new(ARGF.read).to_html)
|
data/ext/array.c
ADDED
@@ -0,0 +1,300 @@
|
|
1
|
+
/* array.c - automatic dynamic array for pointers */
|
2
|
+
|
3
|
+
/*
|
4
|
+
* Copyright (c) 2008, Natacha Porté
|
5
|
+
*
|
6
|
+
* Permission to use, copy, modify, and distribute this software for any
|
7
|
+
* purpose with or without fee is hereby granted, provided that the above
|
8
|
+
* copyright notice and this permission notice appear in all copies.
|
9
|
+
*
|
10
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
11
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
12
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
13
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
14
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
15
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
16
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
17
|
+
*/
|
18
|
+
|
19
|
+
#include "array.h"
|
20
|
+
|
21
|
+
#include <string.h>
|
22
|
+
|
23
|
+
|
24
|
+
/***************************
|
25
|
+
* STATIC HELPER FUNCTIONS *
|
26
|
+
***************************/
|
27
|
+
|
28
|
+
/* arr_realloc • realloc memory of a struct array */
|
29
|
+
static int
|
30
|
+
arr_realloc(struct array* arr, int neosz) {
|
31
|
+
void* neo;
|
32
|
+
neo = realloc(arr->base, neosz * arr->unit);
|
33
|
+
if (neo == 0) return 0;
|
34
|
+
arr->base = neo;
|
35
|
+
arr->asize = neosz;
|
36
|
+
if (arr->size > neosz) arr->size = neosz;
|
37
|
+
return 1; }
|
38
|
+
|
39
|
+
|
40
|
+
/* parr_realloc • realloc memory of a struct parray */
|
41
|
+
static int
|
42
|
+
parr_realloc(struct parray* arr, int neosz) {
|
43
|
+
void* neo;
|
44
|
+
neo = realloc(arr->item, neosz * sizeof (void*));
|
45
|
+
if (neo == 0) return 0;
|
46
|
+
arr->item = neo;
|
47
|
+
arr->asize = neosz;
|
48
|
+
if (arr->size > neosz) arr->size = neosz;
|
49
|
+
return 1; }
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
/***************************
|
54
|
+
* GENERIC ARRAY FUNCTIONS *
|
55
|
+
***************************/
|
56
|
+
|
57
|
+
/* arr_adjust • shrink the allocated memory to fit exactly the needs */
|
58
|
+
int
|
59
|
+
arr_adjust(struct array *arr) {
|
60
|
+
return arr_realloc(arr, arr->size); }
|
61
|
+
|
62
|
+
|
63
|
+
/* arr_free • frees the structure contents (buf NOT the struct itself) */
|
64
|
+
void
|
65
|
+
arr_free(struct array *arr) {
|
66
|
+
if (!arr) return;
|
67
|
+
free(arr->base);
|
68
|
+
arr->base = 0;
|
69
|
+
arr->size = arr->asize = 0; }
|
70
|
+
|
71
|
+
|
72
|
+
/* arr_grow • increases the array size to fit the given number of elements */
|
73
|
+
int
|
74
|
+
arr_grow(struct array *arr, int need) {
|
75
|
+
if (arr->asize >= need) return 1;
|
76
|
+
else return arr_realloc(arr, need); }
|
77
|
+
|
78
|
+
|
79
|
+
/* arr_init • initialization of the contents of the struct */
|
80
|
+
void
|
81
|
+
arr_init(struct array *arr, size_t unit) {
|
82
|
+
arr->base = 0;
|
83
|
+
arr->size = arr->asize = 0;
|
84
|
+
arr->unit = unit; }
|
85
|
+
|
86
|
+
|
87
|
+
/* arr_insert • inserting nb elements before the nth one */
|
88
|
+
int
|
89
|
+
arr_insert(struct array *arr, int nb, int n) {
|
90
|
+
char *src, *dst;
|
91
|
+
size_t len;
|
92
|
+
if (!arr || nb <= 0 || n < 0
|
93
|
+
|| !arr_grow(arr, arr->size + nb))
|
94
|
+
return 0;
|
95
|
+
if (n < arr->size) {
|
96
|
+
src = arr->base;
|
97
|
+
src += n * arr->unit;
|
98
|
+
dst = src + nb * arr->unit;
|
99
|
+
len = (arr->size - n) * arr->unit;
|
100
|
+
memmove(dst, src, len); }
|
101
|
+
arr->size += nb;
|
102
|
+
return 1; }
|
103
|
+
|
104
|
+
|
105
|
+
/* arr_item • returns a pointer to the n-th element */
|
106
|
+
void *
|
107
|
+
arr_item(struct array *arr, int no) {
|
108
|
+
char *ptr;
|
109
|
+
if (!arr || no < 0 || no >= arr->size) return 0;
|
110
|
+
ptr = arr->base;
|
111
|
+
ptr += no * arr->unit;
|
112
|
+
return ptr; }
|
113
|
+
|
114
|
+
|
115
|
+
/* arr_newitem • returns the index of a new element appended to the array */
|
116
|
+
int
|
117
|
+
arr_newitem(struct array *arr) {
|
118
|
+
if (!arr_grow(arr, arr->size + 1)) return -1;
|
119
|
+
arr->size += 1;
|
120
|
+
return arr->size - 1; }
|
121
|
+
|
122
|
+
|
123
|
+
/* arr_remove • removes the n-th elements of the array */
|
124
|
+
void
|
125
|
+
arr_remove(struct array *arr, int idx) {
|
126
|
+
if (!arr || idx < 0 || idx >= arr->size) return;
|
127
|
+
arr->size -= 1;
|
128
|
+
if (idx < arr->size) {
|
129
|
+
char *dst = arr->base;
|
130
|
+
char *src;
|
131
|
+
dst += idx * arr->unit;
|
132
|
+
src = dst + arr->unit;
|
133
|
+
memmove(dst, src, (arr->size - idx) * arr->unit); } }
|
134
|
+
|
135
|
+
|
136
|
+
/* arr_sorted_find • O(log n) search in a sorted array, returning entry */
|
137
|
+
void *
|
138
|
+
arr_sorted_find(struct array *arr, void *key, array_cmp_fn cmp) {
|
139
|
+
int mi, ma, cu, ret;
|
140
|
+
char *ptr = arr->base;
|
141
|
+
mi = -1;
|
142
|
+
ma = arr->size;
|
143
|
+
while (mi < ma - 1) {
|
144
|
+
cu = mi + (ma - mi) / 2;
|
145
|
+
ret = cmp(key, ptr + cu * arr->unit);
|
146
|
+
if (ret == 0) return ptr + cu * arr->unit;
|
147
|
+
else if (ret < 0) ma = cu;
|
148
|
+
else /* if (ret > 0) */ mi = cu; }
|
149
|
+
return 0; }
|
150
|
+
|
151
|
+
|
152
|
+
/* arr_sorted_find_i • O(log n) search in a sorted array,
|
153
|
+
* returning index of the smallest element larger than the key */
|
154
|
+
int
|
155
|
+
arr_sorted_find_i(struct array *arr, void *key, array_cmp_fn cmp) {
|
156
|
+
int mi, ma, cu, ret;
|
157
|
+
char *ptr = arr->base;
|
158
|
+
mi = -1;
|
159
|
+
ma = arr->size;
|
160
|
+
while (mi < ma - 1) {
|
161
|
+
cu = mi + (ma - mi) / 2;
|
162
|
+
ret = cmp(key, ptr + cu * arr->unit);
|
163
|
+
if (ret == 0) {
|
164
|
+
while (cu < arr->size && ret == 0) {
|
165
|
+
cu += 1;
|
166
|
+
ret = cmp(key, ptr + cu * arr->unit); }
|
167
|
+
return cu; }
|
168
|
+
else if (ret < 0) ma = cu;
|
169
|
+
else /* if (ret > 0) */ mi = cu; }
|
170
|
+
return ma; }
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
/***************************
|
175
|
+
* POINTER ARRAY FUNCTIONS *
|
176
|
+
***************************/
|
177
|
+
|
178
|
+
/* parr_adjust • shrinks the allocated memory to fit exactly the needs */
|
179
|
+
int
|
180
|
+
parr_adjust(struct parray* arr) {
|
181
|
+
return parr_realloc (arr, arr->size); }
|
182
|
+
|
183
|
+
|
184
|
+
/* parr_free • frees the structure contents (buf NOT the struct itself) */
|
185
|
+
void
|
186
|
+
parr_free(struct parray *arr) {
|
187
|
+
if (!arr) return;
|
188
|
+
free (arr->item);
|
189
|
+
arr->item = 0;
|
190
|
+
arr->size = 0;
|
191
|
+
arr->asize = 0; }
|
192
|
+
|
193
|
+
|
194
|
+
/* parr_grow • increases the array size to fit the given number of elements */
|
195
|
+
int
|
196
|
+
parr_grow(struct parray *arr, int need) {
|
197
|
+
if (arr->asize >= need) return 1;
|
198
|
+
else return parr_realloc (arr, need); }
|
199
|
+
|
200
|
+
|
201
|
+
/* parr_init • initialization of the struct (which is equivalent to zero) */
|
202
|
+
void
|
203
|
+
parr_init(struct parray *arr) {
|
204
|
+
arr->item = 0;
|
205
|
+
arr->size = 0;
|
206
|
+
arr->asize = 0; }
|
207
|
+
|
208
|
+
|
209
|
+
/* parr_insert • inserting nb elements before the nth one */
|
210
|
+
int
|
211
|
+
parr_insert(struct parray *parr, int nb, int n) {
|
212
|
+
char *src, *dst;
|
213
|
+
size_t len, i;
|
214
|
+
if (!parr || nb == 0 || n < 0
|
215
|
+
|| !parr_grow(parr, parr->size + nb))
|
216
|
+
return 0;
|
217
|
+
if (n < parr->size) {
|
218
|
+
src = (void *)parr->item;
|
219
|
+
src += n * sizeof (void *);
|
220
|
+
dst = src + nb * sizeof (void *);
|
221
|
+
len = (parr->size - n) * sizeof (void *);
|
222
|
+
memmove(dst, src, len);
|
223
|
+
for (i = 0; i < (size_t)nb; ++i)
|
224
|
+
parr->item[n + i] = 0; }
|
225
|
+
parr->size += nb;
|
226
|
+
return 1; }
|
227
|
+
|
228
|
+
|
229
|
+
/* parr_pop • pops the last item of the array and returns it */
|
230
|
+
void *
|
231
|
+
parr_pop(struct parray *arr) {
|
232
|
+
if (arr->size <= 0) return 0;
|
233
|
+
arr->size -= 1;
|
234
|
+
return arr->item[arr->size]; }
|
235
|
+
|
236
|
+
|
237
|
+
/* parr_push • pushes a pointer at the end of the array (= append) */
|
238
|
+
int
|
239
|
+
parr_push(struct parray *arr, void *i) {
|
240
|
+
if (!parr_grow(arr, arr->size + 1)) return 0;
|
241
|
+
arr->item[arr->size] = i;
|
242
|
+
arr->size += 1;
|
243
|
+
return 1; }
|
244
|
+
|
245
|
+
|
246
|
+
/* parr_remove • removes the n-th element of the array and returns it */
|
247
|
+
void *
|
248
|
+
parr_remove(struct parray *arr, int idx) {
|
249
|
+
void* ret;
|
250
|
+
int i;
|
251
|
+
if (!arr || idx < 0 || idx >= arr->size) return 0;
|
252
|
+
ret = arr->item[idx];
|
253
|
+
for (i = idx+1; i < arr->size; ++i)
|
254
|
+
arr->item[i - 1] = arr->item[i];
|
255
|
+
arr->size -= 1;
|
256
|
+
return ret; }
|
257
|
+
|
258
|
+
|
259
|
+
/* parr_sorted_find • O(log n) search in a sorted array, returning entry */
|
260
|
+
void *
|
261
|
+
parr_sorted_find(struct parray *arr, void *key, array_cmp_fn cmp) {
|
262
|
+
int mi, ma, cu, ret;
|
263
|
+
mi = -1;
|
264
|
+
ma = arr->size;
|
265
|
+
while (mi < ma - 1) {
|
266
|
+
cu = mi + (ma - mi) / 2;
|
267
|
+
ret = cmp(key, arr->item[cu]);
|
268
|
+
if (ret == 0) return arr->item[cu];
|
269
|
+
else if (ret < 0) ma = cu;
|
270
|
+
else /* if (ret > 0) */ mi = cu; }
|
271
|
+
return 0; }
|
272
|
+
|
273
|
+
|
274
|
+
/* parr_sorted_find_i • O(log n) search in a sorted array,
|
275
|
+
* returning index of the smallest element larger than the key */
|
276
|
+
int
|
277
|
+
parr_sorted_find_i(struct parray *arr, void *key, array_cmp_fn cmp) {
|
278
|
+
int mi, ma, cu, ret;
|
279
|
+
mi = -1;
|
280
|
+
ma = arr->size;
|
281
|
+
while (mi < ma - 1) {
|
282
|
+
cu = mi + (ma - mi) / 2;
|
283
|
+
ret = cmp(key, arr->item[cu]);
|
284
|
+
if (ret == 0) {
|
285
|
+
while (cu < arr->size && ret == 0) {
|
286
|
+
cu += 1;
|
287
|
+
ret = cmp(key, arr->item[cu]); }
|
288
|
+
return cu; }
|
289
|
+
else if (ret < 0) ma = cu;
|
290
|
+
else /* if (ret > 0) */ mi = cu; }
|
291
|
+
return ma; }
|
292
|
+
|
293
|
+
|
294
|
+
/* parr_top • returns the top the stack (i.e. the last element of the array) */
|
295
|
+
void *
|
296
|
+
parr_top(struct parray *arr) {
|
297
|
+
if (arr == 0 || arr->size <= 0) return 0;
|
298
|
+
else return arr->item[arr->size - 1]; }
|
299
|
+
|
300
|
+
/* vim: set filetype=c: */
|