refactor: use the built-in max/min to simplify the code (#6060)

Signed-off-by: grandpig <grandpig@outlook.com>
This commit is contained in:
grandpig 2026-07-02 08:35:47 +08:00 committed by GitHub
parent 0e1d821fb8
commit 76aee4e177
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 6 additions and 24 deletions

View file

@ -98,10 +98,7 @@ func WebMOpusToWAV(input []byte) ([]byte, error) {
} }
} }
skip := preSkip * channels skip := min(preSkip*channels, len(pcm))
if skip > len(pcm) {
skip = len(pcm)
}
pcm = pcm[skip:] pcm = pcm[skip:]
return encodeWAV(pcm, opusOutputSampleRate, channels), nil return encodeWAV(pcm, opusOutputSampleRate, channels), nil

View file

@ -236,10 +236,7 @@ func (s *APIV1Service) ListMemos(ctx context.Context, request *v1pb.ListMemosReq
return nil, status.Errorf(codes.InvalidArgument, "invalid page token: %v", err) return nil, status.Errorf(codes.InvalidArgument, "invalid page token: %v", err)
} }
limit = normalizePageSize(pageToken.Limit) limit = normalizePageSize(pageToken.Limit)
offset = int(pageToken.Offset) offset = max(int(pageToken.Offset), 0)
if offset < 0 {
offset = 0
}
} else { } else {
limit = normalizePageSize(request.PageSize) limit = normalizePageSize(request.PageSize)
} }
@ -782,10 +779,7 @@ func (s *APIV1Service) ListMemoComments(ctx context.Context, request *v1pb.ListM
return nil, status.Errorf(codes.InvalidArgument, "invalid page token: %v", err) return nil, status.Errorf(codes.InvalidArgument, "invalid page token: %v", err)
} }
limit = normalizePageSize(pageToken.Limit) limit = normalizePageSize(pageToken.Limit)
offset = int(pageToken.Offset) offset = max(int(pageToken.Offset), 0)
if offset < 0 {
offset = 0
}
} else { } else {
limit = normalizePageSize(request.PageSize) limit = normalizePageSize(request.PageSize)
} }

View file

@ -524,10 +524,7 @@ func deleteUserBatches[T any](values []T, size int) [][]T {
batches := make([][]T, 0, (len(values)+size-1)/size) batches := make([][]T, 0, (len(values)+size-1)/size)
for start := 0; start < len(values); start += size { for start := 0; start < len(values); start += size {
end := start + size end := min(start+size, len(values))
if end > len(values) {
end = len(values)
}
batches = append(batches, values[start:end]) batches = append(batches, values[start:end])
} }
return batches return batches

View file

@ -487,10 +487,7 @@ func deleteUserBatches[T any](values []T, size int) [][]T {
batches := make([][]T, 0, (len(values)+size-1)/size) batches := make([][]T, 0, (len(values)+size-1)/size)
for start := 0; start < len(values); start += size { for start := 0; start < len(values); start += size {
end := start + size end := min(start+size, len(values))
if end > len(values) {
end = len(values)
}
batches = append(batches, values[start:end]) batches = append(batches, values[start:end])
} }
return batches return batches

View file

@ -487,10 +487,7 @@ func deleteUserBatches[T any](values []T, size int) [][]T {
batches := make([][]T, 0, (len(values)+size-1)/size) batches := make([][]T, 0, (len(values)+size-1)/size)
for start := 0; start < len(values); start += size { for start := 0; start < len(values); start += size {
end := start + size end := min(start+size, len(values))
if end > len(values) {
end = len(values)
}
batches = append(batches, values[start:end]) batches = append(batches, values[start:end])
} }
return batches return batches