+// splitEntities cuts text into parts with splitMessage, and gives each part the
+// entities in it, with offsets from the start of the part. An entity that
+// crosses a cut becomes one entity in each part.
+func splitEntities(text string, entities []botEntity, limit int) []botPart {
+ var parts []botPart
+ start := 0
+ for _, part := range splitMessage(text, limit) {
+ end := start + utf16Units(part)
+ var kept []botEntity
+ for _, entity := range entities {
+ from := max(entity.Offset, start)
+ to := min(entity.Offset+entity.Length, end)
+ if to <= from {
+ continue
+ }
+
+ entity.Offset = from - start
+ entity.Length = to - from
+ kept = append(kept, entity)
+ }
+
+ parts = append(parts, botPart{
+ text: part,
+ entities: kept,
+ })
+
+ start = end
+ }
+
+ return parts
+}
+