nutools/lib/nulib/awk/base.array.awk

158 lines
3.8 KiB
Awk
Raw Normal View History

2018-04-26 23:19:17 +04:00
# -*- coding: utf-8 mode: awk -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
function mkindices(values, indices, i, j) {
array_new(indices);
j = 1;
2018-04-26 23:19:17 +04:00
for (i in values) {
indices[j++] = int(i);
2018-04-26 23:19:17 +04:00
}
return asort(indices);
2018-04-26 23:19:17 +04:00
}
function array_new(dest) {
dest[0] = 0; # forcer awk à considérer dest comme un tableau
delete dest;
2018-04-26 23:19:17 +04:00
}
function array_newsize(dest, size, i) {
dest[0] = 0; # forcer awk à considérer dest comme un tableau
delete dest;
size = int(size);
2018-04-26 23:19:17 +04:00
for (i = 1; i <= size; i++) {
dest[i] = "";
2018-04-26 23:19:17 +04:00
}
}
function array_len(values, count, i) {
# length(array) a un bug sur awk 3.1.5
# cette version est plus lente mais fonctionne toujours
count = 0;
2018-04-26 23:19:17 +04:00
for (i in values) {
count++;
2018-04-26 23:19:17 +04:00
}
return count;
2018-04-26 23:19:17 +04:00
}
function array_copy(dest, src, count, indices, i) {
array_new(dest);
count = mkindices(src, indices);
2018-04-26 23:19:17 +04:00
for (i = 1; i <= count; i++) {
dest[indices[i]] = src[indices[i]];
2018-04-26 23:19:17 +04:00
}
}
function array_getlastindex(src, count, indices) {
count = mkindices(src, indices);
if (count == 0) return 0;
return indices[count];
2018-04-26 23:19:17 +04:00
}
function array_add(dest, value, lastindex) {
lastindex = array_getlastindex(dest);
dest[lastindex + 1] = value;
2018-04-26 23:19:17 +04:00
}
function array_deli(dest, i, l) {
i = int(i);
if (i == 0) return;
l = array_len(dest);
2018-04-26 23:19:17 +04:00
while (i < l) {
dest[i] = dest[i + 1];
i++;
2018-04-26 23:19:17 +04:00
}
delete dest[l];
2018-04-26 23:19:17 +04:00
}
function array_del(dest, value, ignoreCase, i) {
do {
i = key_index(value, dest, ignoreCase);
if (i != 0) array_deli(dest, i);
} while (i != 0);
2018-04-26 23:19:17 +04:00
}
function array_extend(dest, src, count, lastindex, indices, i) {
lastindex = array_getlastindex(dest);
count = mkindices(src, indices);
2018-04-26 23:19:17 +04:00
for (i = 1; i <= count; i++) {
dest[lastindex + i] = src[indices[i]];
2018-04-26 23:19:17 +04:00
}
}
function array_fill(dest, i) {
array_new(dest);
2018-04-26 23:19:17 +04:00
for (i = 1; i <= NF; i++) {
dest[i] = $i;
2018-04-26 23:19:17 +04:00
}
}
function array_getline(src, count, indices, i, j) {
$0 = "";
count = mkindices(src, indices);
2018-04-26 23:19:17 +04:00
for (i = 1; i <= count; i++) {
j = indices[i];
$j = src[j];
2018-04-26 23:19:17 +04:00
}
}
function array_appendline(src, count, indices, i, nf, j) {
count = mkindices(src, indices);
nf = NF;
2018-04-26 23:19:17 +04:00
for (i = 1; i <= count; i++) {
j = nf + indices[i];
$j = src[indices[i]];
2018-04-26 23:19:17 +04:00
}
}
function in_array(value, values, ignoreCase, i) {
if (ignoreCase) {
value = tolower(value);
2018-04-26 23:19:17 +04:00
for (i in values) {
if (tolower(values[i]) == value) return 1;
2018-04-26 23:19:17 +04:00
}
} else {
for (i in values) {
if (values[i] == value) return 1;
2018-04-26 23:19:17 +04:00
}
}
return 0;
2018-04-26 23:19:17 +04:00
}
function key_index(value, values, ignoreCase, i) {
if (ignoreCase) {
value = tolower(value);
2018-04-26 23:19:17 +04:00
for (i in values) {
if (tolower(values[i]) == value) return int(i);
2018-04-26 23:19:17 +04:00
}
} else {
for (i in values) {
if (values[i] == value) return int(i);
2018-04-26 23:19:17 +04:00
}
}
return 0;
2018-04-26 23:19:17 +04:00
}
function array2s(values, prefix, sep, suffix, noindices, first, i, s) {
if (!prefix) prefix = "[";
if (!sep) sep = ", ";
if (!suffix) suffix = "]";
s = prefix;
first = 1;
2018-04-26 23:19:17 +04:00
for (i in values) {
if (first) first = 0;
else s = s sep;
if (!noindices) s = s "[" i "]=";
s = s values[i];
2018-04-26 23:19:17 +04:00
}
s = s suffix;
return s;
2018-04-26 23:19:17 +04:00
}
function array2so(values, prefix, sep, suffix, noindices, count, indices, i, s) {
if (!prefix) prefix = "[";
if (!sep) sep = ", ";
if (!suffix) suffix = "]";
s = prefix;
count = mkindices(values, indices);
2018-04-26 23:19:17 +04:00
for (i = 1; i <= count; i++) {
if (i > 1) s = s sep;
if (!noindices) s = s "[" indices[i] "]=";
s = s values[indices[i]];
2018-04-26 23:19:17 +04:00
}
s = s suffix;
return s;
2018-04-26 23:19:17 +04:00
}
function array_join(values, sep, prefix, suffix, count, indices, i, s) {
s = prefix;
count = mkindices(values, indices);
2018-04-26 23:19:17 +04:00
for (i = 1; i <= count; i++) {
if (i > 1) s = s sep;
s = s values[indices[i]];
2018-04-26 23:19:17 +04:00
}
s = s suffix;
return s;
2018-04-26 23:19:17 +04:00
}