move html stripping out of field_checksum into caller

This commit is contained in:
Damien Elmes 2020-03-17 14:52:55 +10:00
parent 7eab504126
commit cc54e92756
2 changed files with 22 additions and 13 deletions

View file

@ -40,9 +40,10 @@ impl Note {
}
}
/// Text must be passed to strip_html_preserving_image_filenames() by
/// caller prior to passing in here.
pub(crate) fn field_checksum(text: &str) -> u32 {
let text = strip_html_preserving_image_filenames(text);
let digest = sha1::Sha1::from(text.as_ref()).digest().bytes();
let digest = sha1::Sha1::from(text).digest().bytes();
u32::from_be_bytes(digest[..4].try_into().unwrap())
}
@ -119,15 +120,20 @@ pub(super) fn set_note(db: &Connection, note: &mut Note, note_type: &NoteType) -
note.mtime_secs = i64_unix_secs();
// hard-coded for now
note.usn = -1;
let csum = field_checksum(&note.fields()[0]);
let sort_field = strip_html_preserving_image_filenames(
note.fields()
.get(note_type.sort_field_idx as usize)
.ok_or_else(|| AnkiError::DBError {
info: "sort field out of range".to_string(),
kind: DBErrorKind::MissingEntity,
})?,
);
let field1_nohtml = strip_html_preserving_image_filenames(&note.fields()[0]);
let csum = field_checksum(field1_nohtml.as_ref());
let sort_field = if note_type.sort_field_idx == 0 {
field1_nohtml
} else {
strip_html_preserving_image_filenames(
note.fields()
.get(note_type.sort_field_idx as usize)
.ok_or_else(|| AnkiError::DBError {
info: "sort field out of range".to_string(),
kind: DBErrorKind::MissingEntity,
})?,
)
};
let mut stmt =
db.prepare_cached("update notes set mod=?,usn=?,flds=?,sfld=?,csum=? where id=?")?;

View file

@ -4,7 +4,9 @@
use super::parser::{Node, PropertyKind, SearchNode, StateKind, TemplateKind};
use crate::card::CardQueue;
use crate::notes::field_checksum;
use crate::{collection::RequestContext, types::ObjID};
use crate::{
collection::RequestContext, text::strip_html_preserving_image_filenames, types::ObjID,
};
use rusqlite::types::ToSqlOutput;
use std::fmt::Write;
@ -240,7 +242,8 @@ fn write_single_field(ctx: &mut SearchContext, field: &str, val: &str) {
}
fn write_dupes(ctx: &mut SearchContext, ntid: &ObjID, text: &str) {
let csum = field_checksum(text);
let text_nohtml = strip_html_preserving_image_filenames(text);
let csum = field_checksum(text_nohtml.as_ref());
write!(
ctx.sql,
"(n.mid = {} and n.csum = {} and field_at_index(n.flds, 0) = ?",