Files
ziran/std/rss_scan.zi
T

150 lines
4.7 KiB
Ziran

#import "byte_text_linux"
FeedSpan :: struct {
start: s32
end: s32
after: s32
valid: bool
}
FeedMatch :: (document: string, at: s32, needle: string) -> bool {
if at < 0 || at + cast(s32)needle.count > cast(s32)document.count {
return false
}
return document[at:at + cast(s32)needle.count] == needle
}
#program_export
FeedFind :: (document: string, from: s32, needle: string) -> s32 {
at: s32 = from
while at + cast(s32)needle.count <= cast(s32)document.count {
if FeedMatch(document, at, needle) { return at }
at += 1
}
return -1
}
#program_export
FeedNextItem :: (document: string, from: s32) -> FeedSpan {
result: FeedSpan
at: s32 = FeedFind(document, from, "<item")
if at < 0 { return result }
open_end: s32 = FeedFind(document, at, ">")
end: s32 = FeedFind(document, open_end + 1, "</item>")
if open_end < 0 || end < 0 { return result }
result.start = open_end + 1
result.end = end
result.after = end + 7
result.valid = true
return result
}
#program_export
FeedTag :: (document: string, item: FeedSpan, tag: string) -> FeedSpan {
result: FeedSpan
if !item.valid { return result }
at: s32 = item.start
while at < item.end {
at = FeedFind(document, at, "<")
if at < 0 || at >= item.end { return result }
if at + 1 + cast(s32)tag.count < item.end &&
FeedMatch(document, at + 1, tag) {
following: u8 = document[at + 1 + cast(s32)tag.count]
if following == cast(u8)62 || following == cast(u8)32 {
open_end: s32 = FeedFind(document, at, ">")
if open_end < 0 || open_end >= item.end { return result }
close_at: s32 = open_end + 1
while close_at < item.end {
close_at = FeedFind(document, close_at, "</")
if close_at < 0 || close_at >= item.end { return result }
if FeedMatch(document, close_at + 2, tag) &&
close_at + 2 + cast(s32)tag.count < item.end &&
document[close_at + 2 + cast(s32)tag.count] ==
cast(u8)62 { break }
close_at += 2
}
if close_at >= item.end { return result }
result.start = open_end + 1
result.end = close_at
result.after = close_at + 3 + cast(s32)tag.count
result.valid = true
return result
}
}
at += 1
}
return result
}
FeedEntity :: (document: string, at: s32, end: s32, output: *u8) -> s32 {
if at + 5 <= end && FeedMatch(document, at, "&amp;") {
output.* = cast(u8)38
return 5
}
if at + 4 <= end && FeedMatch(document, at, "&lt;") {
output.* = cast(u8)60
return 4
}
if at + 4 <= end && FeedMatch(document, at, "&gt;") {
output.* = cast(u8)62
return 4
}
if at + 6 <= end && FeedMatch(document, at, "&quot;") {
output.* = cast(u8)34
return 6
}
if at + 6 <= end && FeedMatch(document, at, "&apos;") {
output.* = cast(u8)39
return 6
}
return 0
}
// Copies a text or CDATA field into caller-owned storage. Tags in summaries
// become spaces, and a malformed/truncated field is rejected.
#program_export
FeedCopyText :: (document: string, span: FeedSpan, output: []u8) -> s32 {
if !span.valid || output.count == 0 { return -1 }
start: s32 = span.start
end: s32 = span.end
if FeedMatch(document, start, "<![CDATA[") &&
end - start >= 12 && FeedMatch(document, end - 3, "]]>") {
start += 9
end -= 3
}
count: s32 = 0
in_tag: bool = false
at: s32 = start
while at < end {
ch: u8 = document[at]
if ch == cast(u8)60 {
in_tag = true
if count > 0 && output[count - 1] != cast(u8)32 {
if count + 1 >= cast(s32)output.count { return -1 }
output[count] = cast(u8)32
count += 1
}
}
if !in_tag && ch == cast(u8)38 {
decoded: u8 = 0
consumed: s32 = FeedEntity(document, at, end, *decoded)
if consumed > 0 {
ch = decoded
at += consumed - 1
}
}
if !in_tag {
if ch == cast(u8)9 || ch == cast(u8)10 || ch == cast(u8)13 {
ch = cast(u8)32
}
if count + 1 >= cast(s32)output.count { return -1 }
output[count] = ch
count += 1
}
if ch == cast(u8)62 { in_tag = false }
at += 1
}
output[count] = cast(u8)0
return count
}