// TrytesToTrits converts a slice of trytes into trits.
13752
func TrytesToTrits(trytes Trytes) (Trits, error) {
	if err := ValidTrytes(trytes); err != nil {
		return nil, err
	}
	trits := make(Trits, len(trytes)*3)
	for i := range trytes {
		idx := strings.Index(TryteAlphabet, string(trytes[i:i+1]))
		copy(trits[i*3:i*3+3], TryteToTritsLUT[idx])
	}
	return trits, nil
}
202798
func TritsEqual(a Trits, b Trits) (bool, error) {
	if err := ValidTrits(a); err != nil {
		return false, err
	}
	if err := ValidTrits(b); err != nil {
		return false, err
	}

	if len(a) != len(b) {
		return false, nil
	}

	for i := range a {
		if a[i] != b[i] {
			return false, nil
		}
	}
	return true, nil
}
1089281
func (c *Curl) MustSqueezeTrytes(length int) Trytes {
	return MustTritsToTrytes(c.MustSqueeze(length))
}
644639
func SortSampleItems(sc *stmtctx.StatementContext, items []*SampleItem) error {
	sorter := sampleItemSorter{items: items, sc: sc}
	sort.Stable(&sorter)
	return sorter.err
}
379405
func BytesToTrytes(bytes []byte, numTrytes ...int) (Trytes, error) {
	numTrits := []int{}
	if len(numTrytes) > 0 {
		numTrits = append(numTrits, numTrytes[0]*3)
	}

	trits, err := BytesToTrits(bytes, numTrits...)
	if err != nil {
		return "", err
	}

	trits = PadTrits(trits, int(nearestGreaterMultipleOfThree(uint64(len(trits)))))
	return TritsToTrytes(trits)
}
1005436
func (d *ManyToOne) TryNext() (data GenericDataType, ok bool) {
	// Read a value from the ring buffer based on the readIndex.
	idx := d.readIndex % uint64(len(d.buffer))
	result := (*bucket)(atomic.SwapPointer(&d.buffer[idx], nil))

	// When the result is nil that means the writer has not had the
	// opportunity to write a value into the diode. This value must be ignored
	// and the read head must not increment.
	if result == nil {
		return nil, false
	}

	// When the seq value is less than the current read index that means a
	// value was read from idx that was previously written but has since has
	// been dropped. This value must be ignored and the read head must not
	// increment.
	//
	// The simulation for this scenario assumes the fast forward occurred as
	// detailed below.
	//
	// 5. The reader reads again getting seq 5. It then reads again expecting
	//    seq 6 but gets seq 2. This is a read of a stale value that was
	//    effectively "dropped" so the read fails and the read head stays put.
	//    `| 4 | 5 | 2 | 3 |` r: 7, w: 6
	//
	if result.seq < d.readIndex {
		return nil, false
	}

	// When the seq value is greater than the current read index that means a
	// value was read from idx that overwrote the value that was expected to
	// be at this idx. This happens when the writer has lapped the reader. The
	// reader needs to catch up to the writer so it moves its write head to
	// the new seq, effectively dropping the messages that were not read in
	// between the two values.
	//
	// Here is a simulation of this scenario:
	//
	// 1. Both the read and write heads start at 0.
	//    `| nil | nil | nil | nil |` r: 0, w: 0
	// 2. The writer fills the buffer.
	//    `| 0 | 1 | 2 | 3 |` r: 0, w: 4
	// 3. The writer laps the read head.
	//    `| 4 | 5 | 2 | 3 |` r: 0, w: 6
	// 4. The reader reads the first value, expecting a seq of 0 but reads 4,
	//    this forces the reader to fast forward to 5.
	//    `| 4 | 5 | 2 | 3 |` r: 5, w: 6
	//
	if result.seq > d.readIndex {
		dropped := result.seq - d.readIndex
		d.readIndex = result.seq
		d.alerter.Alert(int(dropped))
	}

	// Only increment read index if a regular read occurred (where seq was
	// equal to readIndex) or a value was read that caused a fast forward
	// (where seq was greater than readIndex).
	//
	d.readIndex++
	return result.data, true
}
1301227
func (k *Kerl) MustSqueezeTrytes(length int) Trytes {
	return MustTritsToTrytes(k.MustSqueeze(length))
}
499627
func ReadAllTargets(t Targeter) (tgts []Target, err error) {
	for {
		var tgt Target
		if err = t(&tgt); err == ErrNoTargets {
			break
		} else if err != nil {
			return nil, err
		}
		tgts = append(tgts, tgt)
	}

	if len(tgts) == 0 {
		return nil, ErrNoTargets
	}

	return tgts, nil
}
1124253
func TryToTime(i interface{}) (t time.Time, err error) {
	switch v := i.(type) {
	case time.Time:
		t = v
	case *time.Time:
		t = *v
	case string:
		t, err = StringToTime(v)
	case *string:
		t, err = StringToTime(*v)
	default:
		err = castError(i, "time.Time")
	}
	return
}
559833
func tryConvert(convertor runtime.ObjectConvertor, object runtime.Object, versions ...string) (runtime.Object, error) {
	var last error
	for _, version := range versions {
		if len(version) == 0 {
			return object, nil
		}
		obj, err := convertor.ConvertToVersion(object, version)
		if err != nil {
			last = err
			continue
		}
		return obj, nil
	}
	return nil, last
}
502368
func TryToInt(i interface{}) (r int, err error) {
	switch t := i.(type) {
	case nil:
		r = 0
	case int:
		r = t
	case *int:
		r = *t
	case int8:
		r = int(t)
	case int16:
		r = int(t)
	case int32:
		r = int(t)
	case int64:
		r = int(t)
	case uint:
		r = int(t)
	case uint8:
		r = int(t)
	case uint16:
		r = int(t)
	case uint32:
		r = int(t)
	case uint64:
		r = int(t)
	case float32:
		r = int(t)
	case float64:
		r = int(t)
	case bool:
		if t {
			r = 1
		}
	case string:
		r, err = strconv.Atoi(t)
	default:
		err = castError(i, "int")
	}
	return
}
759722
func (k *Kerl) SqueezeTrytes(length int) (Trytes, error) {
	trits, err := k.Squeeze(length)
	if err != nil {
		return "", err
	}
	return TritsToTrytes(trits)
}
1314058
func ASCIIToTrytes(s string) (Trytes, error) {
	if !asciiRegex.MatchString(s) {
		return "", ErrInvalidASCIIInput
	}

	trytesStr := ""

	for _, c := range s {
		trytesStr += string(TryteAlphabet[c%27])
		trytesStr += string(TryteAlphabet[(c-c%27)/27])
	}

	return NewTrytes(trytesStr)
}
462147
func NewTrytes(s string) (Trytes, error) {
	err := ValidTrytes(s)
	return s, err
}
573605
func TrytesToInt(t Trytes) int64 {
	var val int64

	for i := len(t) - 1; i >= 0; i-- {
		idx := strings.Index(TryteAlphabet, string(t[i]))
		if idx > 13 {
			idx = idx - 27
		}
		val = val*27 + int64(idx)
	}
	return val
}
1047662
func (c *Curl) AbsorbTrytes(inn Trytes) error {
	var in Trits
	var err error

	if len(inn) == 0 {
		in = Trits{0}
	} else {
		in, err = TrytesToTrits(inn)
		if err != nil {
			return err
		}
	}
	return c.Absorb(in)
}
371112
func (api *API) SendTrytes(trytes []Trytes, depth uint64, mwm uint64, reference ...Hash) (bundle.Bundle, error) {
	if err := Validate(ValidateTransactionTrytes(trytes...)); err != nil {
		return nil, err
	}
	tips, err := api.GetTransactionsToApprove(depth, reference...)
	if err != nil {
		return nil, err
	}
	trytes, err = api.AttachToTangle(tips.TrunkTransaction, tips.BranchTransaction, mwm, trytes)
	if err != nil {
		return nil, err
	}
	trytes, err = api.StoreAndBroadcast(trytes)
	if err != nil {
		return nil, err
	}
	return transaction.AsTransactionObjects(trytes, nil)
}